JTable或JXTable中的列分隔符 [英] Column dividers in JTable or JXTable

查看:159
本文介绍了JTable或JXTable中的列分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable(或更准确地说是JXTable),其中有3个要划分的分组列部分. 我曾经有3个以编程方式链接的表(滚动条位置,排序,选择).我使用了很多代码来实现此链接,而我想摆脱它. 现在,我将切换到1个JXTable,因为此表类中的某些内容要好得多.

I Have a JTable (or a JXTable to be more precise) with 3 sections of grouped columns I want to divide. I used to have 3 tables which i programmatically linked (the scrollbar position, the sorting, the selection). I used a lot of code to get this linked, and I want to get rid of this. Now I Am switching to 1 JXTable, because there are some things a lot nicer in this table class.

我发现了几乎相同问题的一些解决方案(不是很令人满意). 也许有人对我有很好的建议.

I found some (not very satisfying) solutions to almost the same problem. Maybe some one has a good suggestion for me.

选项1:将一个空列用作分隔符(另一种颜色,例如灰色),并在使用箭头或Tab键时以编程方式跳过该空列.

Option 1: an empty column as a divider (another color, like gray) and programatically hop over this empty column when using the arrows or tab keys.

选项2:将1列的仅一侧的边距设置为较大的尺寸,因此它看起来像是分隔线.直到现在,我才发现如何为所有列设置边距

option 2: setting the margin for just 1 side of 1 column to a larger size, so it seems like a divider. Untill now I have only found out how to set the margins for all columns

选项3:再次回到3个单独的表(特别是要以相同的方式对表进行排序是很多工作,因为我不想在单独的部分中重复这些列).这意味着我必须重写我的表排序器,对不可见的列进行排序.

option 3: getting back to 3 seperate tables again (especially to get the tables sorted in the same way is a lot of work, because I do not want to repeat the columns in the seperate sections). This means I have to rewrite my table sorter, sorting on a non-visible column.

任何建议都是可以接受的(也可以是三个给定选项中的一个都不可以)

any suggestion is welcome (also if it's none of the three given options)

推荐答案

我通过覆盖第三列的单元格渲染器以使其具有较粗的右边框,并且没有其他内容,从而使外观看起来与您想要的东西相似边界.您可以在表列标题中执行相同的操作,以使边框向上延伸.显然是在单元格内放置了边框,但这对您来说可能就足够了.

I've made something that looks somewhat like what you're going for by overriding the cell renderer on the 3rd column to have a thick right border and no other borders. You could do the same within the table column header to have the border extend up through there. It's clearly placing the border within the cell but this may be sufficient for you.

  {
    ....
    table.getColumnModel().getColumn(2).setCellRenderer(
        new ThickRightBorderCellRenderer());
    ....
  }

  private static class ThickRightBorderCellRenderer
          extends DefaultTableCellRenderer {

    @Override
    public Border getBorder() {
      return BorderFactory.createMatteBorder(0, 0, 0, 3, Color.BLACK);
    }
  }



这篇关于JTable或JXTable中的列分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆