How to make a columns in JTable Invisible for Swing Java

前端 未结 8 1474
半阙折子戏
半阙折子戏 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);
    

提交回复
热议问题