Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox as Cell renderer in JTable

 
Lon Allen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Swamy Vatti
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Similar to the way u added a cell editor(JCombo), u have to add a cell renderer(JCombo) to the table.
 
Lon Allen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Lon Allen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic