1

I have a JTable where one column has a row-unique list of items with a JComboBox editor. (There are no event listeners involved.) The problem is that the cells are not displaying JComboBoxes, but just as JLabels of the underlying list's toString. I have a class that implements TableModel which on isCellEditable returns true for that column. Rhe cells do not respond to the mouse.

I have tried having getColumnClass return both JComboBox and the class of the underlying list, and in both cases I still just get the list's toString. I haven't made much headway in trying to make my own renderer because 1. The Oracle tutorial "How to Use Tables" is one of the most incomprehensible programming web pages ever written, and 2. I strongly suspect that I am "doing it wrong" since surely swing already knows how to render a JComboBox of Strings. Doesn't it?

The desired behavior is that they appear to be JComboBoxes and can be edited with the mouse. I sense I should be posting more details but I don't know what I should be posting. Thanks in advance.

Community
  • 1
  • 1
Mr. Wallet
  • 17
  • 4

1 Answers1

1

A JComboBox may be used as a cell editor, but you want a similar appearance for the cell renderer. Two alternative approaches for this are examined here:

  • Add an arrow to a DefaultTableCellRenderer.

  • Use a ComboBoxRenderer that extends JComboBox and implements TableCellRenderer.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I attempted to use the method for adding an arrow and it doesn't seem to have had any effect. Furthermore, that doesn't seem to have anything to do with resolving the issue of not displaying a proper default value and not responding to the mouse. – Mr. Wallet Oct 23 '13 at 21:19
  • Please edit your question to include an [sscce](http://sscce.org/) that shows your current approach; I started from @aterai's working [example](http://stackoverflow.com/a/12844115/230513). – trashgod Oct 23 '13 at 21:26
  • Could not repro problem on simpler example. Will work out what I'm doing wrong and then refer to [Putting JComboBox into JTable](http://stackoverflow.com/questions/457463/putting-jcombobox-into-jtable) – Mr. Wallet Oct 23 '13 at 22:30
  • See also [*Examples that Use Tables*](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#eg). – trashgod Oct 24 '13 at 00:58