1

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,

maloney
  • 1,633
  • 3
  • 26
  • 49

2 Answers2

2
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

If you want to disabled the edition of a cell in a table, you should override TableModel.isCellEditable(int,int)

Here all you are doing is render a disabled JComboBox but this does not prevent edition, it just renders a disabled JComboBox. See also http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Excellent - thanks for the help! The problem was i was already overriding that method in the model to always return true if the combobox was selected. Now fixed to only return true if its enabled. – maloney Oct 30 '12 at 11:33