可分组表头

时间:2016-05-23 09:56:46

标签: java

我在面板上添加了JTable,我需要它来分组列。我实现了here给出的已知解决方案但我的代码出了问题,表格没有显示。

以下是代码:

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

0 个答案:

没有答案