10

Possible Duplicate:
How to make a columns in JTable Invisible for Swing Java
How to hide a particlar column in DefaultTableModel from displaying it in table?

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

How can I hide a column in this manner?

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();
 }
Michael Peyper
  • 6,814
  • 2
  • 27
  • 44
Adesh singh
  • 2,083
  • 9
  • 24
  • 36
  • 2
    Possible duplicate of [How to hide a particlar column in DefaultTableModel from displaying it in table?](http://stackoverflow.com/questions/12195973/how-to-hide-a-particlar-column-in-defaulttablemodel-from-displaying-it-in-table) or [this question](http://stackoverflow.com/questions/10088853/could-not-set-the-column-width-to-zero-i-e-not-made-column-invisible/10089138#10089138) or [this one](http://stackoverflow.com/questions/8371328/jtable-hide-and-show-columns) or .. – Andrew Thompson Dec 20 '12 at 13:50
  • 1
    consider [SwingX](http://swingx.java.net): its JXTable/TableColumnExt support in/visible columns – kleopatra Dec 20 '12 at 13:50
  • possible duplicate [How to make a columns in JTable Invisible for Swing Java](http://stackoverflow.com/q/1492217/813853). –  Dec 20 '12 at 13:58
  • In the design pattern MVC, when you say Storing data you do that in the Model part of the MVC. Hiding a column from a user, that means you delete it from View part of the model MVC. –  Dec 20 '12 at 14:06

1 Answers1

31

Set the Column Minimum and Maximum width as zero.

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

As suggested by Andrew Thomson in the comment section you can also use removeColumn.

From javaDoc;

removeColumn

public void removeColumn(TableColumn aColumn) 

Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it. Parameters: aColumn - the TableColumn to be removed

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

Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • @ALJI Mohamed please undelete your answer here, quite correct, and ignore wrong action by donw_voter – mKorbel Dec 20 '12 at 14:31
  • 1
    I like the set width approach. Although I would consider also making in un-resizable to avoid the user accidentally showing it: `table.getColumnModel().getColumn(columnIndex).setResizable(false);` – OLL Oct 29 '13 at 09:46
  • 1
    This actually helps especially when getting the id from `JTable` – Francisunoxx Feb 13 '17 at 06:49