Column dividers in JTable or JXTable
Asked Answered
M

1

3

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)

Medamedal answered 3/1, 2012 at 13:1 Comment(5)
I found already these discussions: https://mcmap.net/q/1925002/-freeze-columns-in-jxtable and #2614957 (option 1)Medamedal
For your option 2 you can use the JTable#prepareRenderer to alter the component for a specific row/column and for example add an empty border around a specific side of the component.Aerodontia
@Robin: thanks for the comment and suggestion. Dave answered approx. the same, but I like his answer better because of the code and example screenshot.Medamedal
@Aerodontia - wrong for JXTable (as you know ;-) SwingX way to apply visual decorations are HiglightersPrimateship
@Primateship Indeed. I almost forgot about those. Should urgently freshen up my SwingX knowledgeAerodontia
C
4

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

Conglutinate answered 3/1, 2012 at 13:41 Comment(4)
Thank you for the example, even with screenshot! This could do the trick... already voting you up, before I select this as The answer, I want to wait for more / other suggestions... and try this out...Medamedal
I have played a bit with it, and this is exactly the thing I needed, using the same trick on the header is also a good suggestion!Medamedal
for a JXTable, never-ever subclass DefaultTableCellRenderer. Instead, implement a Highlighter doing the decorationPrimateship
@kleopatra: any exmaple of how to do this? (just 1 border I mean)Medamedal

© 2022 - 2024 — McMap. All rights reserved.