Reputation: 21
I am a software developer apprentice and have to write a graphical project specific configuration editor for my company. I load the data from the configuration excel file of the project with Apache POI and wrap the data into ConfigValue Objects. For different ConfigValue objects there has to be different cell editors and renderers...
The GUI of my programm uses a custom JTable and DefaultTableModel. Every value in the table / model is a ConfigValue which should rendered differently for defined different ConfigTypes. (As far I got it all working - import, wrapping, load into table)
But I have some problems with the TableCellRenderer
or TableCellEditor
of one of the custom types which should rendered as a ComboBox which contains all possible backend entity values. The ComboBox gets rendered and the correct beginning values are displayed... But when I change one cell to another ConfigValue... The renderer does not display this value... (it always changes to the same value (first of the editor's value) for a cell)
Can anyone help me out what I am doing wrong with my Editor/Renderer?
public class ConfComboBoxCellEditor extends DefaultCellEditor {
public ConfComboBoxCellEditor(List<ConfigValue> possibleValues) {
super(new JComboBox(possibleValues.toArray()));
}
@Override
public Object getCellEditorValue() {
Object cellEditorValue = super.getCellEditorValue();
System.out.println("DEBUG - CELL EDITOR - get editor value --> " + ((ConfigValue) cellEditorValue).toString());
return cellEditorValue;
}
}
public class ConfComboBoxCellRenderer extends JComboBox<ConfigValue> implements TableCellRenderer {
public ConfComboBoxCellRenderer() {
System.out.println("NEW CELL RENDERER");
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
ConfComboBoxCellRenderer renderer = (ConfComboBoxCellRenderer) table.getCellRenderer(row, column);
renderer.removeAllItems();
renderer.addItem((ConfigValue) value);
renderer.setSelectedItem(value);
System.out.println("DEBUG - CELL RENDERER " + row + ", " + column + " - get cell render comp --> " + ((ConfigValue) value));
return this;
}
}
Upvotes: 2
Views: 898
Reputation: 109815
Can anyone help me out what I am doing wrong with my Editor/Renderer?
JTable support JComboBox as TableCellEditor, there isn't any issue to set different dataset for each of JComboBoxes used as TableCellEditor
TableCellRenderer only shows, painting the value stored in DefaultTableModel, then every code lines inside renderer.xxxXxx are missinterpreting of Renderers Concept in Swing, contraproductive and could be heavy tasks, Renderer isn't place to set/getValue, a new event is fired from all mouse/key events from all cell are visible in JViewport, plus internal events from JTable/TableModel APIs,
your Renderer isn't about how to painting JComboBox as rendering component
nothing cleaver, nor specifics without an SSCCE/MCVE, short runnable, compilable with hardcoded value for JTable/DefaultTableModel in local variable
Upvotes: 1