How to make a columns in JTable Invisible for Swing Java

前端 未结 8 1418
半阙折子戏
半阙折子戏 2020-12-03 04:44

I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . How should I do that ?

相关标签:
8条回答
  • 2020-12-03 05:04

    If you remove the column from the JTable the column is still present in the TableModel.

    For example to remove the first ID column:

    TableColumnModel tcm = table.getColumnModel();
    tcm.removeColumn(tcm.getColumn(0));
    

    If you want to access the value of the removed column, you have to access it through the getValueAt function of the TableModel, not the JTable. But you have to convert to rowIndex back to rowIndex in the model.

    For example if you want to access the first column of the selected row:

    int modelRow = table.convertRowIndexToModel(table.getSelectedRow());
    int value = (Integer)table.getModel().getValueAt(modelRow,0);
    
    0 讨论(0)
  • 2020-12-03 05:09

    i had the same problem and because of i am using TableColumnModel removColumn(); does'not help me so i used this

    table.getColumnModel().getColumn(0).setWidth(0);
    table.getColumnModel().getColumn(0).setMinWidth(0);
    table.getColumnModel().getColumn(0).setMaxWidth(0); 
    

    and worked fine for me it hide a column 0 and i still able to get value from it

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