Question

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.

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.

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

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)

Was it helpful?

Solution

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);
    }
  }

Example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top