Hi,
I have written a combobox cell editor and renderer for a jtable cell, which actually works.
But when i select en entry in the box, the value is not populated to the table cell. Can anyone guess why?
Thank you for any suggestions!
Regards,
Andreas
public class CellRendererWithMPComboBox extends mpv5.ui.beans.LightMPComboBox implements TableCellRenderer {
private final Context c;
private final JTable table;
/**
* Create a new CellRenderer which holds a MPComboBox. Will not assign itself to any column.
* @param c
* @param table
*/
public CellRendererWithMPComboBox(Context c, JTable table) {
this.c = c;
this.table = table;
}
/**
* Set this renderer to the given column
* @param column
*/
public void setRendererTo(int column) {
TableColumn col = table.getColumnModel().getColumn(column);
col.setCellEditor(new MPComboBoxEditor(c));
col.setCellRenderer(this);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (hasFocus) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
try {
// getComboBox().setSelectedIndex(1);
setSelectedItem(((MPComboBoxModelItem)value).getValue());
} catch (Exception e) {
Log.Debug(e);
}
return this;
} else {
return new DefaultCellRenderer();
}
}
class MPComboBoxEditor extends DefaultCellEditor {
private final Context c;
public MPComboBoxEditor(Context c) {
super(new mpv5.ui.beans.MPCombobox(c, table).getComboBox());
this.c = c;
}
}
}