Hello,
i have a JTable with 2 columns. The first column is "static". But i want to enable the user to select the value of the 2nd column from a ComboBox editor. The thing is that the combo box items depends on the first column (for a row).
So:
- i can't use:
jTable.setDefaultEditor(MyObjct.class, new DefaultCellEditor(staticComboBox));
- i can't make this work:
jTable.setDefaultEditor(MyObject.class, new MyObjectEditor());
public class MyObjectEditor extends AbstractCellEditor implements TableCellEditor,
ActionListener {
private MyObject o;
public Object getCellEditorValue() {
return o;
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
o = (MyObject)value;
// creat a JComboBox, depending on the argument
// column 0 -> static object
// column 1 -> combo box
return getCombo((MyObject)table.getValueAt(row, 0));
}
}
Any help apreciated.
Mr Smith - 14 Mar 2005 16:37 GMT
> Hello,
>
> i have a JTable with 2 columns. The first column is "static". But i want to enable the user to select the value of the 2nd column from a ComboBox editor. The thing is that the combo box items depends on the first column (for a row).
responding myself, this is working:
public class MyObjectEditor extends DefaultCellEditor {
public MyObjectEditor() {
super(new JComboBox(new Object[] { null }));
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
fillCombo((JComboBox)editorComponent, (MyObject)table.getValueAt(row, 0));
((JComboBox)editorComponent).setSelectedItem(value);
return editorComponent;
}
}