JTable hide and show columns

后端 未结 6 859
南旧
南旧 2021-01-11 13:03

I want to add some columns to a table (Swing JTable). Some of them will have a default size (e.g. 250), others will be hidden (so their size will be 0). I use this code:

6条回答
  •  星月不相逢
    2021-01-11 13:14

    try this something like this for example:

    myTableModel = new DefaultTableModel();
    myTableModel.setColumnIdentifiers(new Object[]{"ID", "Name"});
    JTable myTable = new JTable(myTableModel);
    
    // remember to save the references
    TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
    TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
    //...
    
    // remove temporary the column ("hide")
    myTable.getColumnModel().removeColumn(myTableColumn1);
    
    // then you restore that column when you need it ("show")
    myTable.getColumnModel().addColumn(myTableColumn1);
    

    That's the best way I know to hide a column.

提交回复
热议问题