Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top