• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to hide a column in JTable view

 
David Winters Junior
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use JTable's removeColumn(TableColumn).
It removes the column only from the view.
 
David Winters Junior
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree this does not change the model, but it is confusing. This helped me:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic