This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes how to hide a column in JTable view Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to hide a column in JTable view" Watch "how to hide a column in JTable view" New topic
Author

how to hide a column in JTable view

David Winters Junior
Ranch Hand

Joined: Oct 30, 2007
Posts: 47
Hi All,

I am having an issue at present whereby i need to ba able to hide from the user the data in a particular column in the Jtable as the data is not for user viewing purposes.

I have a seperate class for the TableModel which represent the data which is stored in the JTable so

TableModelClass temp = new TableModelClass;
JTableClass main = new JTable(temp);

However i only want to display 9 of the 10 columns in the JTable. Does anyone know the code which will allow me to do this. By the way i need to just hide this column data from the user but it needs to be in the JTable as
the column data contains primary keys which are used for data retrieval in other parts of the application.

Is there any code to size the reduce a particular column width size to zero?

Any help would be appreciated
david
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

In JTable you should find a method that returns the table's TableColumnModel. Notice that TableColumnModel has a removeColumn() method.

That's probably the easiest way to do it for now. But another possibility would be for your table model to be structured so it's one object per row, and that object provides 9 methods to return the 9 visible columns. You could store the primary key data in that object without having to allocate an invisible column to it.
David Winters Junior
Ranch Hand

Joined: Oct 30, 2007
Posts: 47
Thanks Paul for the reply.

I have decided to use the TableColumn api which has a setMaxwidth() method which will allow me to set the column width to zero.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/table/TableColumn.html

The issue here is that when the user selects a particular row in the table and clicks on the remove button i need to be able to access the primary key field related to that row so that i can use this value to remove the row in the underlying database.

David
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

You seem to be under the impression that removeColumn() removes the column from the model. I don't believe it does, I believe it only removes it from the view.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Paul Clapham:
You seem to be under the impression that removeColumn() removes the column from the model. I don't believe it does, I believe it only removes it from the view.


This is the second person I've seen who is reluctant to use removeColumn() for some reason, even though it does exactly what they want. This must means Sun named it poorly?


bitguru blog
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Originally posted by Brian Cole:
This is the second person I've seen who is reluctant to use removeColumn() for some reason, even though it does exactly what they want. This must means Sun named it poorly?
The API documentation doesn't help, either. All it says is "Deletes the TableColumn column from the tableColumns array." I don't know what array that is either, but I'm pretty comfortable with the MVC pattern and it's intuitively clear to me that it's talking about the view. But I certainly wouldn't claim that was obvious.

Nothing else in the API documentation for TableColumnModel says that it is part of the view, and in fact (now I see what you mean) its name suggests it's part of the model.
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
You can also use JTable's removeColumn(TableColumn).
It removes the column only from the view.


Nikos' Java blog
David Winters Junior
Ranch Hand

Joined: Oct 30, 2007
Posts: 47
Thanks paul, brian and nikos for the replies.

Yes i had assumed that this api this remove it from the model also but since it only removes it from the view this is exactly what i need.

Thanks again for reaffirming that.

David
Frank Wynants
Greenhorn

Joined: Oct 17, 2007
Posts: 4
Check out JXTable from Swinglabs

It has a user control for making columns visible/invisible. You can also make columns visible/invisible from within your software.
M Simpson
Greenhorn

Joined: Dec 09, 2009
Posts: 2
I agree this does not change the model, but it is confusing. This helped me:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to hide a column in JTable view