views:

1964

answers:

3

Hello, 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 ?

A: 

Set the min, max and "normal" width to 0:

jTable.getColumn("ABC").setWidth(0);
jTable.getColumn("ABC").setMinWidth(0);
jTable.getColumn("ABC").setMaxWidth(0);

The second approach is to extend TableColumnModel and override all the methods to create the illusion (for the JTable) that your model doesn't have those two columns.

So when you hide a column, you must return one less when the table asks for the number of columns and when it asks for the column X, you may have to add +1 to the column index (depending on whether it is to the left or right of the hidden column), etc.

Let your new table model forward all method calls (with the corrected indexes, etc) to the actual column model and use the new table model in the JTable.

Aaron Digulla
No, it do not work.
om
You must also set min and max width.
Aaron Digulla
A: 

You could create a subclass of the model, and override TableModel.getColumnCount as follows:

int getColumnCount() { return super.getColumnCount()-2; }

The last two columns would then not be displayed in the JTable.

Maurice Perry
but can I get the data from them ?
om
you're only creating the illusion that the columns aren't there, but the are still there and should thus be able to get the data still
dharga
But how should I hide them from GUI.
om
Not sure I understand what you mean by that. Do you mean that the user of you application should be able to hide or show?
Maurice Perry
Hey I solved that issue by using following code jtable.removeColumn(jtable.getColumnModel().getColumn()); Column gets hide from GUI and we can also get the stored value from it.
om
+4  A: 

Remove the TableColumn from the TableColumnModel.

If you need access to the data then you use table.getModel().getValueAt(...).

camickr
this is the real right way to do this - setting the width to 0 is kind of bogus. This way you still have the data in your model; you're just hiding part of the view.
M1EK
Even with the above approach the data is still in the model which is why you can access it as demonstrated above. This is how MVC works. All we are doing is changing the view. When you set the width to 0, try tabbing, when you hit the hidden column focus disappears until you tab again. This will confuse users.
camickr