*
The moose likes Swing / AWT / SWT and the fly likes JCombobox in JTable and lookup values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JCombobox in JTable and lookup values" Watch "JCombobox in JTable and lookup values" New topic
Author

JCombobox in JTable and lookup values

Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 100
Hi all,
My first post in more than half a year or so... :-)

I am working on a new Swing application. I encoutner a problem which should be trivial but keeps on bothering me... :-(
In my object model, I have a property (The datatype of some published content) which can have a few discrete values, therefore I declared it as an int property and defined some constants for it.
For each row in the JTable, the user must be able to select a value for this property, i use a JCombobox for this purpose.
Of course, the user does not want to see the value of the integer contant, but a readable description.
To achieve this, I defined a ComboBoxEntry class holding both the id (int value) and the description of the item, the toString method is overrided to return the description. Because JComboBox uses the toString method to render its values, I can add ComboBoxEntry instances to it, which are then displayed as descriptions.

To render the particular JTable column as a combobox, I set it as the renderer for this column:


In the setValueAt method of the TableModel, I write the selected value backto the object model as follows:


This works. But when the value is not edited in the table, it is still displayed as a number. I know that I must add an appropriate renderer, but do not know how to read the string value of the selected item...

To summarize, I want to edit a numeric property as a lookup field in my table, and render it as a string. This seems to be a trivial and often needed fucntionality to me, but the implementation gives me a headache...
Any tips ???
TIA, Klaas


"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JCombobox in JTable and lookup values
 
Similar Threads
JComboBox as Cell renderer in JTable
Jtable combo box issue
JTable selected row problem
JTable
JTable