Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JCombobox in JTable and lookup values  RSS feed

Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!