0

I have JTable added on panel and I need it to have grouped columns. I Implemented the known solutions given here but something is wrong with my code, table is not shown.

Here is the code:

public void createTable(){
    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][]{
      {"119","foo","bar","ja","ko","zh"},
      {"911","bar","foo","en","fr","pt"}},
    new Object[]{"SNo.","1","2","Native","2","3"});

    table = new JTable( dm ) {
        protected JTableHeader createDefaultTableHeader() {
            return new GroupableTableHeader(columnModel);
        }
      };

    TableColumnModel cm = table.getColumnModel();
    ColumnGroup g1 = new ColumnGroup("Group 1");
    g1.add(cm.getColumn(1));
    g1.add(cm.getColumn(2));
    ColumnGroup g2 = new ColumnGroup("Group 2");
    g2.add(cm.getColumn(3));
    g2.add(cm.getColumn(4));

    GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
    header.addColumnGroup(g1);
    header.addColumnGroup(g2);
}
Community
  • 1
  • 1
  • And you added the table to what and how? – MadProgrammer May 23 '16 at 10:15
  • Ah my bad I forgot to scrollPane.setViewportView(table); Everything is good now except when I use Nimbus Look and Feel the column headers don't have lines to resize them like in normal metal theme. Any idea for that? – Ђорђе Ђашић May 23 '16 at 10:19
  • Yeah, the implementation of group header sucks. What I mean by that is, it uses the basic ui delegate, and basically sucks. You have s look at [this](http://stackoverflow.com/questions/20206578/show-column-header-on-dragging-column/20208995#20208995) instead – MadProgrammer May 23 '16 at 11:03

0 Answers0