How to hide a column in the DefaultTableModel column with its display in the table?

I am using the Java Swingx framework. I have 4 columns in my DefaultTableModel object. I want to display only 3 columns. But for calculation I need all four.

Actual Data Model

S.No. | ID | GDC ID | Decsription

What I want to display in the table

S.No.| GDC ID | Decsription

Is it possible to hide or omit only one column from rendering? Please guide me.

+2
java swing swingx jtable defaulttablemodel
source share
5 answers
  • default minimum size is 10 pixels widht,

  • you can remove / add a column from the JTable view, the column is presented in XxxTableModel , you can hide and show any column

+4
source share

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

Removes aColumn from this JTable column array. Note: this method does not remove the data column from the model; it just removes the TableColumn, which is responsible for displaying it.

Also note the existence of the following methods:

Since the order of the columns and columns in the view ( JTable ) may differ from the one in the model, you need these methods to switch between the view and the model

+4
source share

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); 
+2
source share

try deleting one 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 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 this is the best way I know.

+1
source share

You control getValueAt, getColumnCount to achieve this.

So for example getColumnCount you give it as 3

and on getValueAt - skip if the column index is above the missing column

JTable will first call getColumnCount and getRowCount and select a getValueAt call for each of the cells.

NOTE. Ignore this and see the trashgod link below.

0
source share

All Articles