【问题标题】:disable Combobox in jtable by click on checkbox通过单击复选框禁用 jtable 中的组合框
【发布时间】:2012-10-31 13:30:43
【问题描述】:

我正在尝试在 java 中做一些简单的应用程序。我在 jTable 中渲染了一些 CheckBox 和 ComboBox。现在我正在尝试对该项目进行工作,例如获取价值、启用-禁用组合框。但是我遇到了一些问题。
我现在面临什么
1.
我在 jTable 中渲染 ComboBox 和 CheckBox。当我单击相应行的复选框时,我正在尝试启用 ComboBox。如果我的复选框未启用,那么它应该禁用组合框。
我试过但没有成功。

2
我正在尝试单击复选框,但如果我使用 setSelected,则所有复选框都会被选中,但是当我试图取消选中它时,它没有。
这是我的代码供您参考。

    public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
    public MyComboBoxRenderer(String[] items) {
        super(items);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(Color.BLACK);
            super.setBackground(Color.WHITE);
        } else {
            setForeground(Color.BLACK);
            setBackground(Color.WHITE);
        }

        // Select the current value
        setSelectedItem(value);
        return this;
    }
   }

    public class MyComboBoxEditor extends DefaultCellEditor {
    public MyComboBoxEditor(String[] items) {
        super(new JComboBox(items));
     }
   }

.

     public class MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
     public MyCheckBoxRenderer(String[] items) {
        super();
       // setSelected(true);
     }

     public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(Color.BLACK);
            super.setBackground(Color.WHITE);
        } else {
            setForeground(Color.BLACK);
            setBackground(Color.WHITE);
        }

       // setSelected(true);
        // Select the current value

        return this;
    }
    }

    public class MyCheckBoxEditor extends DefaultCellEditor {
    public MyCheckBoxEditor() {
        super(new JCheckBox());

} 
}

给我一​​些提示或参考。
提前致谢。

【问题讨论】:

    标签: java swing jtable jcombobox jcheckbox


    【解决方案1】:
    猜你喜欢
    • 2014-02-12
    • 2019-06-08
    • 2011-08-13
    • 1970-01-01
    • 2015-05-12
    • 2016-10-07
    • 1970-01-01
    • 2015-01-13
    • 2011-12-28
    相关资源
    最近更新 更多