• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

JCombobox in JTable and lookup values

Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Bookmark Topic Watch Topic
  • New Topic