How to make a columns in JTable Invisible for Swing Java

97,026

Solution 1

Remove the TableColumn from the TableColumnModel.

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

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

For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager.

Solution 2

First remove the column from the view

 table.removeColumn(table.getColumnModel().getColumn(4));

Then retrieve the data from the model.

table.getModel().getValueAt(table.getSelectedRow(),4);

One thing to note is that when retrieving the data, it must be retrieve from model not from the table.

Solution 3

I tried 2 possible solutions that both work, but got some issue with the 1st solution.

   table.removeColumn(table.getColumnModel().getColumn(4));

or

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

In my recent case, I preferred the 2nd solution because I added a TableRowSorter.

   TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
   table.setRowSorter(sorter);

When using table.removeColumn(table.getColumnModel().getColumn(4)), it will physically remove the column from the view/table so you cannot use table.getValueAt(row, 4) - it returns ArrayIndexOutOfBounds. The only way to get the value of the removed column is by calling table.getModel().getValueAt(table.getSelectedRow(),4). Since TableRowSorter sorts only what's on the table but not the data in the DefaultTableModel object, the problem is when you get the value after sorting the records - it will retrieve the data from DefaultModelObject, which is not sorted.

So I used the 2nd solution then used table.getValueAt(table.getSelectedRow(),4);

The only issue I see with the 2nd approach was mentioned by @camickr: "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."

Solution 4

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

Solution 5

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

jTable.getColumn("ABC").setMinWidth(0); // Must be set before maxWidth!!
jTable.getColumn("ABC").setMaxWidth(0);
jTable.getColumn("ABC").setWidth(0);

Note: Since you can't set a maxWidth < minWidth, you need to change minWidth, first (javadoc). Same is true for width.

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.

Share:
97,026

Related videos on Youtube

om.
Author by

om.

Updated on July 09, 2022

Comments

  • om.
    om. 5 months

    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 ?

  • om.
    om. about 13 years
    but can I get the data from them ?
  • dharga
    dharga about 13 years
    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
  • om.
    om. about 13 years
    But how should I hide them from GUI.
  • Muhammad Ahmad Afzal about 13 years
    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?
  • om.
    om. about 13 years
    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.
  • M1EK about 13 years
    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.
  • camickr
    camickr about 13 years
    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.
  • Dima
    Dima about 11 years
    This works except one thing which makes this approach useless. When you want to know which is your selected column you will get the wrong (shifted) index of the column. In other words, doing table.getColumnModel().getSelectedColumns() will give you a wrong column.
  • Dima
    Dima about 11 years
    Actually I was wrong... Doing column.getModelIndex() give the correct position in the model. Just don't use table.getSelectedColumn() - this will give wrong index.
  • camickr
    camickr about 11 years
    @Dima you need to understand the difference between the view and the model. Table methods always refer to the view. The JTable API has a bunch of convertXXX methods you can use. So you could have used the convertColumnIndexToModel(...)
  • kleopatra
    kleopatra over 10 years
    @xmedeko a really complete solution is to use SwingX :-)

Related