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;
}
}