aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes making a JTable column not visible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "making a JTable column not visible" Watch "making a JTable column not visible" New topic
Author

making a JTable column not visible

Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
Hi,

I need to keep some data in a column of a JTable, but I don't want that column to be visible to the user. For example, a database ID column should be populated for each row, but not made visible to the user.

I can't seem to find a table.getColumn("id").setVisible(false) kind of solution. Any ideas?

Cheers, Jared.


SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you want the column itself visible, something like this might work



if you don't want the column visible, something like this
table.removeColumn(table.getColumnModel().getColumn(0));

where 0 is the column number.
This removes it from the display area of the table, but not the model.
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
Originally posted by Michael Dunn:
if you don't want the column visible, something like this
table.removeColumn(table.getColumnModel().getColumn(0));

where 0 is the column number.
This removes it from the display area of the table, but not the model.[/QB]


Thanks, I have been trying this but it seems to be removing the column from the underlying model too. If I do:

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

The column does not appear in the JTable anymore, however if I then try:

Object o = table.getValueAt(row, 6); // row changes depending the selection

I get an index out of range exception:

java.lang.ArrayIndexOutOfBoundsException: 6 >= 6

I know I've done this before, but something has gone astray ...

Cheers, Jared.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
change
Object o = table.getValueAt(row, 6);

to
Object o = dtm.getValueAt(row, 6);
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
That's the one. Thanks a million.

Cheers, Jared.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: making a JTable column not visible