my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes TableCellEditor ComboBox in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TableCellEditor ComboBox in JTable" Watch "TableCellEditor ComboBox in JTable" New topic
Author

TableCellEditor ComboBox in JTable

John Forsyth
Greenhorn

Joined: May 30, 2007
Posts: 9
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

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
John Forsyth
Greenhorn

Joined: May 30, 2007
Posts: 9
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TableCellEditor ComboBox in JTable
 
Similar Threads
Jtable question
JTable ComboBox Cell Renderer/Editor
Row Highlight on a JTable
JTable
GUI Questions: JTable and ComboBox