I have a JTable with a column of JComboBoxes. All of the cells in this column is rendered as a JLabel (the default cell renderer). If you edit one of the cells in this column the editor used is a JComboBox, but once the editor no longer has focus the cell once again is rendered as a JLabel. How do I get the cell renderer to display the cells as comboBoxes all the time? I want the end-user to see that these cells are comboBoxes and don't want to have to rely on toolTips to inform them that they need to click a cell to get the comboBox to appear. Note: I also have checkBoxes in another column and they appear as checkBoxes all the time (which is what I want). If I set the render for these cells to the default, then they display the text "true" or "false" when their values change along with a mometary display of the check box while the editor has focus. I can't figure out how to get the comboBoxes to display as comboBoxes instead of just the text that is selected in the comboBox... Lon Allen
I was able to create a CellRender class that extends DefaultTableCellRender and it displays a combobox in the table. The only trouble I have now is that after the cell editor takes over and lets you change the selected value in the comboBox, when the cell editor loses focus and the tablecellrender takes over the cell returns to rendering as a JLabel again. Apparently since the return value from the cell editor is a string the table then returns to displaying the value as a JLabel. I'm stumped...
GOT IT! I was extending DefaultTableCellRenderer in my new class. It by default wants to render items as a JLabel. Therefore I created my class to extend JComboBox and implement TableCellRenderer and this solved my problem.
I declare myColumnRenderer at the beginning of my code and when I am building my table I set the column's cellRenderer to myColumnRender. I have also set the column's cellEditor to a JComboBox that is instantiated with an Object of the values I wish to display. When I build my table data I also use an Object that looks like the following:
I add the data as rows using the addRow(Object) method suppled with the DefaultTableModel. In this case I have JComboBoxes for the values "None" and this value is the first value in my JComboBoxes. This way it appears that the first item in both JComboBoxes is the initially selected item.
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Free, earth friendly heat - from the CodeRanch trailboss