简体   繁体   English

如何禁用JTable中的特定单元格?

[英]How to disable a particular cell in JTable?

I am working with swing. 我正在挥杆。 I Have a JTable having 8 columns and Dynamic rows. 我有一个JTable具有8列和动态行。 2nd column is non-editable which I did like this in DefaultTableModel . 第二列是不可编辑的,我在DefaultTableModel这样做。

static JComboBox combo1 = new javax.swing.JComboBox(new String[]{"Static","Project Variable", "External", "Output Variable"});
            ParametersTable.setModel(new     javax.swing.table.DefaultTableModel(
            parametersTableData,
            new String[]{
                "S.No", "Parameters", "Parameter Type", "Static Value", "Variable Name", "Sheet Name", "Column Name", "Output Variable"
            }
            ) { 
Class[] types = new Class[]{
                    java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
                };

                public Class getColumnClass(int columnIndex) {
                    return types[columnIndex];
                }

                boolean[] canEdit = new boolean[]{
                    true, false, true, true, true, true, true, true 
                };

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
            }       
            );
            ParametersTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(combo1));

I have a JComboBox in 3rd column which has values static,project variable,external,output variable . 我在第3列中有一个JComboBox ,其值为static,project variable,external,output variable

Assuming there are 2 Rows ,So when I select Parameter Type as static in first row, i want that particular cell (Static Value) to be enabled in that particular row and rest of the cells to be disabled. 假设有2行,那么当我在第一行中将Parameter Type选择为static时,我希望在该特定行中启用该特定cell (静态值),并禁用其余单元格。 Similarly when I select Parameter Type as "Output Variable" in second row. 类似地,当我在第二行中将参数类型选择为“输出变量”时。 I want that particular cell (Output Variable) to be enabled in that particular row and rest of the cells to be disabled. 我希望在该特定行中启用该特定单元格(输出变量),并禁用其余单元格。

Change your isCellEditable as follows. 如下更改isCellEditable

public boolean isCellEditable(int rowIndex, int columnIndex) {
    String comboValue = ParametersTable.getValueAt(rowIndex, 0).toString(); //0 is the column index where your combo box value available.
    if(comboValue.equals("static")){
        return false; //The cell (row, column) will be non editable
    }
    return canEdit[columnIndex];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM