JComboBox shared between multiple table cells automatically selecting currently selected item

本小妞迷上赌 提交于 2019-12-02 07:58:05

By default the first item of a combo box is selected, which in your example is "Red".

When you edit a cell the value from the TableModel is selected in the comboBox editor. Since the data in your table does not match any of the entries in the comboBox, the selection doesn't change so "Red" is displayed as the value in the editor.

When you make a selection from the editor that value is then saved in the model and will be displayed properly the next time you edit the cell.

The solution to your problem is to make sure the TableModel contains valid data when it is created. Only that way can the proper item in the comboBox be selected.

I figured I would have to attach an action listener to the combobox to determine which item to selected

No, you don't play with listeners on a comboBox when it is used as an editor. The comboBox editor does the selection of the item automatically for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!