Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Cell renderer for different JComboBox's in same column?

Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,

My problem is that i want to have JComboBoxs in a column in my table. However, the items in the JComboBox are different, and are set depending on other row data.

Using this article ( i've almost managed to achieve what i want to do.

In that article on adding multiple JTable cell editors per column, the author is able to decide on the items's for his combo box before he needs to display the table. I can't quite do this in my circumstance. After the data for the table is generated in the form of an Object [][], the table is refreshed like so:

This happens in a refreshTable() method i have created. I also make a call to this method after the table model has been set:

From the above code you may see that the JComboBox data in Object[][] comes in the form of already populated DefaultCellEditor's. This potentially could be populated JComboBox's or simply a Object[] of items. I'm using here DefaulCellEditor's here because it makes sense when calling addEditorForRow.

Ok, so all this works fine and as expected, i can click on an individual cell and get the different combo box values. However, this only happens after the individual cell has been clicked. If the cell is not clicked then the cell initially displays the toString value of the DefaulCellEditor. I can understand why it does this, as this is the value the original table model finds in the object[][].

I've played and tried but without success to try and combat this problem. Is there some way i could maybe use some kind of renderer instead of the setCellEditors method that would display this data correct from the first time of asking?

Any comments or advice on how to go about this would be most appreciated.
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the answer in your post. You should use a CellRenderer for the cells with JComboBox CellEditors.

For example:

This method implements the getTableCellRendererComponent from TableCellRenderer. When the column is has a JComboBox CellEditor it returns the value of the JComboBox selected item. When the column has a CheckBox cellEditor it returns the CheckBox and otherwise it returns what a DefaultCellRenderer would return (renderer is a DefaultCellRenderer).

Hope it helps.

Happy Coding!
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic