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

前端 未结 5 398
星月不相逢
星月不相逢 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.

    0 讨论(0)
  • 2020-12-21 12:03
    • by default minimum size is 10 pixels widht,

    • you can to remove / add column from JTable view, column presents in the XxxTableModel, you can to hide and show any of column(s)

    0 讨论(0)
  • 2020-12-21 12:11

    You can hide it by setting its width to 0.

    _table.getColumn("ID").setPreferredWidth(0);
    _table.getColumn("ID").setMinWidth(0);
    _table.getColumn("ID").setWidth(0);
    _table.getColumn("ID").setMaxWidth(0);
    
    0 讨论(0)
  • 2020-12-21 12:13

    You manipulate the getValueAt , getColumnCount to achieve this.

    So for example the getColumnCount you give it as 3

    and on getValueAt - to skip if the column index is above the skipped column

    JTable will first call getColumnCount and getRowCount and fetch calling getValueAt for each of the cells.

    NOTE: Ignore this and see link by trashgod below.

    0 讨论(0)
  • 2020-12-21 12:16

    No need to adjust your model, or to try to make that column very small. JTable has built-in functionality for this: the removeColumn method. As stated in the javadoc of that method

    Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.

    Also note the existence of the following methods:

    • JTable#convertColumnIndexToModel
    • JTable#convertColumnIndexToView

    Since the column order and column count in the view (the JTable) might be different from the one in the model you need those methods to switch between view and model

    0 讨论(0)
提交回复
热议问题