Im having problems setting up a JComboBox. The user is given a few options on a separte panel which determine if a JComboBox should be enabled/disabled - the problem I have is that the user can still select from the JComboBox even when it is disabled (It's disabled as the combobox is greyed out)! The JComboBox uses a custom TableCellRenderer and custom DefaultCellEditor. Also the JComboBox is a cell/column in a row of a JTable.
So here is a breakdown of the code:
*prepareRenderer of the JTable*
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
JComponent component = (JComponent) super.prepareRenderer(renderer, row, column);
//Code which checks to see if component should be enabled
enableComponent = false;
component.setEnabled(enableComponent);
}
*Set up the combobox *
public void setupUserCombo(){
TableColumn col = getColumnModel().getColumn(0);
List<String> comboUsers = new String["Adam", "Ben"]
MyComboBoxRenderer jComboBox = (new MyComboBoxRenderer((String[])values.toArray(comboUsers ));
col.setCellEditor(new MyComboBoxEditor((String[])values.toArray(new String[0])));
col.setCellRenderer(jComboBox);
repaint();
}
*TableCellRenderer *
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public MyComboBoxRenderer(String[] items) {
super(items);
repaint();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setSelectedItem("");
if (isSelected) {
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
}
*DefaultCellEditor *
public class MyComboBoxEditor extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
Any pointers as to what im doing wrong would be greatly appreciated!!
Thanks,