This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

making a JTable column not visible

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change
Object o = table.getValueAt(row, 6);

to
Object o = dtm.getValueAt(row, 6);
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the one. Thanks a million.

Cheers, Jared.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic