禁用后仍可从JComboBox中选择

时间:2022-04-08 11:50:39

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.

我在设置JComboBox时遇到了问题。在separte面板上为用户提供了一些选项,用于确定是否应该启用/禁用JComboBox - 我遇到的问题是,即使禁用了JComboBox,用户仍然可以从JComboBox中进行选择(由于组合框显示为灰色,因此它被禁用)! JComboBox使用自定义TableCellRenderer和自定义DefaultCellEditor。此外,JComboBox是JTable行中的单元格/列。

So here is a breakdown of the code:

所以这里是代码的细分:

*prepareRenderer of the JTable*

* 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 *

* 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 *

* 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,

谢谢,

2 个解决方案

#1


2  

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

如果要禁用表中单元格的版本,则应覆盖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

这里你要做的就是渲染一个禁用的JComboBox但是这不会阻止编辑,它只是渲染一个禁用的JComboBox。另见http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender

#2


2  

  • see JTable tutorial about Using a Combo Box as an Editor

    请参阅有关使用组合框作为编辑器的JTable教程

  • view by @aterai

    查看@aterai

  • don't store JComponents in the XxxTableModel, then last selected value from JComboBox is stored in XxxTableModel as String value

    不要将JComponents存储在XxxTableModel中,然后从JComboBox中最后选择的值作为String值存储在XxxTableModel中

#1


2  

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

如果要禁用表中单元格的版本,则应覆盖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

这里你要做的就是渲染一个禁用的JComboBox但是这不会阻止编辑,它只是渲染一个禁用的JComboBox。另见http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender

#2


2  

  • see JTable tutorial about Using a Combo Box as an Editor

    请参阅有关使用组合框作为编辑器的JTable教程

  • view by @aterai

    查看@aterai

  • don't store JComponents in the XxxTableModel, then last selected value from JComboBox is stored in XxxTableModel as String value

    不要将JComponents存储在XxxTableModel中,然后从JComboBox中最后选择的值作为String值存储在XxxTableModel中