aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How column can be hidden in JTable without removing from model ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How column can be hidden in JTable without removing from model ?" Watch "How column can be hidden in JTable without removing from model ?" New topic
Author

How column can be hidden in JTable without removing from model ?

Krishna Varma Adluru
Ranch Hand

Joined: Nov 24, 2001
Posts: 55
Hi Guys,
How can I hide a table column from JTable without removing it from the underlying table model?
Thank You,
Regards,
Krishna Varma Adluru
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
1) Extend your model from AbstractTableModel
2) Override the method public int getColumnCount() and return the count as total number of columns - 1. You need to move your hidden column to the end.
That's it.
Krishna Varma Adluru
Ranch Hand

Joined: Nov 24, 2001
Posts: 55
Sai,
Thank You .. I will try it out and see how it works.
Thanks again,
Regards,
Krishna Varma Adluru
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Why do you want to hide a column?
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Krishna Varma Adluru
Ranch Hand

Joined: Nov 24, 2001
Posts: 55
Mark,
You are right ..!!! No need to hide column !!!
Previously, I thought "Record Number" will also be
visible in the JTable ...
Thanks,
Regards,
Krishna Varma
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
hi guys,
why do you not need to hide the record number ? (i think i'm missing something)
i have the record number in my table model so when a row is selected, i can pass this identifier to the listener - which uses this to retrieve the appropriate flight record.
thanks, dean
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
hi guys,
when building the array of String[]'s that forms the table's model, I included one extra column that contains the recrod number. i did this so the recrd number can be passed through to the listener code that then uses this to retrieve the appropriate flight recrod from the database (as this is the key field).
i can't really think of any oway around this. Are you assumnig that the row number will equate to the record id ?
Although this would work with the supplied datbase. what if the actual database used in production had a few deleted records ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Dean, isn't there some other means to find a flight in the current Data class. Something that you might find there, that you have overlooked.
I'd suggest looking again and seeing if you can find it. Then post again and tell me what you find.


Mark
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
There are two ways to handle this situation:
1) Store the actual record number from the database which is contained in the DataInfo object as the last column in the table model array and do not show it in GUI. Use this record number and call getRecord() on Data
2) Assuming that you are showing the flight number in one of the columns in GUI, you can use
find() in Data and pass the flight number.
I am using option 1 since it is lot faster than option 2...I don't mean it...you don't see any differnce.
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
hi mark - i feel really thick now becasue all i can see is getRecord and seek - both of which take the record number as a parameter. I suppose could create a search string such as "Flight number=SA001" and pass this to criteriaFind, but as flight number is not a key field this may return a number of recrods and not just the one the user selected in the JTable ?
just had another quick look at the find() implementation. I guess I can change this so it looks for a match in the flight number column - but again this isn't the key field so assuming there were 2 records for SA001 and the user selected the second in the table, the listener would always deal with the first ??
sorry mate - i'm stuck on this one - what's worrying me more is that if i can't spot something when it is handed to me on a plate i've got no chance
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

OK, I tried to help you without giving the answer out blatantly. The find methods does what? find, based on what? the first field. What is the first field in the database?
I guess I can change this so it looks for a match in the flight number column

Any change needed?
Mark
[ April 29, 2002: Message edited by: Mark Spritzler ]
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I think Dean is asking whether flight number column is the primary key for the table or not. The javadoc comments for the find() method says
For this assignment, the key field is the record number field
I think this is a mistake. I believe the key field for this assignment is the flight number since the record number is a derived value rather than a stored value.
[ April 29, 2002: Message edited by: Sai Prasad ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Yes. Flight Number is a Primary Key Candidate. or should be the primary key.
Mark
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
Hi Mark / Sai, thanks for your help with this.
I have been confused by the fact that in the javadoc comment for the find method it say's
This method searches the database for an entry which has a first field which exactly matches the string supplied. If the required record cannot be found, this method returns null. For this assignment the key field is the record number field.

even though inside the implementation of find it looks in values[0], which as you rightly say mark, is the flight number coilumn.
is this a mistake - or something thrown in to purposefully confuse? would you just change the javadoc comment and perhaps mention it in the exam ?
thanks again for your help
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

is this a mistake - or something thrown in to purposefully confuse? would you just change the javadoc comment and perhaps mention it in the exam ?


No to both. I just left it as is. Usually, and I use that loosely. The first field(s) in a table should be what constitutes the primary key.
Mark
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
thansk mate - i was having trouble distinguishing the "primary key" of the database schema - which exists at the record level (the flight number), and the physical key used internally by the database (the record number).
thanks again for your help - it's proving invaluable
Rasika Chitnis
Ranch Hand

Joined: Jun 19, 2001
Posts: 131
I still feel it is safer to use getRecord method that takes record number as the key instead of find method that takes flight number as the key. Even the modify method which uses find method further checks the record number of the record returned by find; if the record number of the record returned by find does not match the record number of the passed in parameter, it throws DatabaseException.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

True Rasika.
However you need to think about why they put the find method in the Data class.
This is because the data does not tell you record number, but the primary unique key field is Flight Number, which is given to you in the data. So you must use it first to find the record.
I agree that record number is what the data class uses to check to make sure it is going to modify the correct record. But like in Oracle, you never use a record ID, you use the primary key
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How column can be hidden in JTable without removing from model ?