How to hide a particlar column in DefaultTableModel from displaying it in table?

前端 未结 5 414
星月不相逢
星月不相逢 2020-12-21 11:31

I am using Java Swingx framework. I have 4 columns in my DefaultTableModel object. I wish to display only 3 of the columns. But, I need all four for computing.<

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 12:00

    try this to remove a single column:

    myTableModel = new DefaultTableModel();
    myTableModel.setColumnIdentifiers(new Object[]{"S.No.", "ID", "GDC ID", "Decsription"});
    JTable myTable = new JTable(myTableModel);
    
    // remember to save the references
    TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
    TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
    TableColumn myTableColumn2 = guiLoteryNumbersTable.getColumnModel().getColumn(2);
    TableColumn myTableColumn3 = guiLoteryNumbersTable.getColumnModel().getColumn(3);
    
    myTable.getColumnModel().removeColumn(myTableColumn1);
    

    Then to show the column again keeping the order:

    // 1- remove all the existent columns
    myTable.getColumnModel().removeColumn(myTableColumn0);
    myTable.getColumnModel().removeColumn(myTableColumn2);
    myTable.getColumnModel().removeColumn(myTableColumn3);
    
    // 2- add all the columns again in the right order
    myTable.getColumnModel().addColumn(myTableColumn0);
    myTable.getColumnModel().addColumn(myTableColumn1);
    myTable.getColumnModel().addColumn(myTableColumn2);
    myTable.getColumnModel().addColumn(myTableColumn3);
    

    Sorry, but that's the best way I know.

提交回复
热议问题