• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TableCellEditor ComboBox in JTable

 
John Forsyth
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on getting a JTable to display a different combo box in each row. I have successfully had this happen, however, there is initially nothing shown in the table cell until I click and select from the options.

My process is to create a new JComboBox with all the options then attach it to the JTable through a TableCellEditor.

I'm not sure if it's a repaint issue or if I need to select the option in the combo box in a certain way. All I need is to have the first option in the combo box shown in the table.
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Forsyth:
I'm working on getting a JTable to display a different combo box in each row. I have successfully had this happen, however, there is initially nothing shown in the table cell until I click and select from the options.

My process is to create a new JComboBox with all the options then attach it to the JTable through a TableCellEditor.

I'm not sure if it's a repaint issue or if I need to select the option in the combo box in a certain way. All I need is to have the first option in the combo box shown in the table.


So the cell renderers are ok? It's just the editor you're having trouble with?

It doesn't sound like a repaint issue. The editor's combo box should be set to match the current value of the cell (from the table model's getValueAt() method), regardless of which index it has. If the current value of the cell doesn't match any of the combo box's items then it will usually act as you describe.

It should be possible to modify the editor so that it will display first combo box item if the value doesn't match any of its items if that's really what you want. Depending on what you are trying to do, it may be better to modify your table model instead.
[ August 04, 2008: Message edited by: Brian Cole ]
 
John Forsyth
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've figured out that my issue was that while I was setting the selected index, I needed to select the actual value. It will now display the first item in the table.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic