簡體   English   中英

通過單擊復選框禁用jtable中的組合框

[英]disable Combobox in jtable by click on checkbox

我試圖在Java中做一些簡單的應用程序。 我在jTable中渲染了一些CheckBox和ComboBox。 現在,我正在嘗試在該項目上進行工作,例如獲取價值,啟用-禁用組合框。 但是我面臨一些問題。
我現在面對的是
1。
我在jTable中渲染ComboBox和CheckBox。 當我單擊相應行的復選框時,我試圖啟用ComboBox。 如果我的復選框未啟用,則應禁用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());

} 
}

給我一些提示或參考。
提前致謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM