Hide column in JTable

Possible duplicate:
How to make columns in JTable Invisible for Swing Java
How to hide a column in the DefaultTableModel column with its display in the table?

I am trying to enter data into three columns in JTable, but I want to show only two columns. In fact, I want to hide the third column, not by setting the width to 0, but by any other method in which I can get data from a hidden column on a click event.

How can I hide a column this way?

I am using the following code:

try { String Title[]= new String{"a","b","c"}; Object obj= new Object[50][3]; JTable table= new JTable(obj,title); JScrollPane jsp= new JScrollPane(table); add(jsp); } catch(Exception ex) { ex.printStackTrace(); } 
+8
java swing jtable jscrollpane
source share
1 answer

Set the minimum and maximum column width to zero.

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

As the link suggested by Andrew Thomson in the comments section, you can also use removeColumn .

From javaDoc;

removeColumn

 public void removeColumn(TableColumn aColumn) 

Removes aColumn from this JTable column array. Note: this method does not remove the data column from the model; it just removes the TableColumn that was responsible for displaying it. Parameters: aColumn - Table for the table to be deleted

PS: But I personally used the first approach to hide the column in JTable . Thanks for the removeColumn method, I will try to use it from now on.

+24
source share

All Articles