2015-05-25 25 views
0

我正在使用秋千。我有一个JTable有8列和动态行。 第二列是不可编辑的,我在DefaultTableModel中这样做过。如何禁用JTable中的特定单元格?

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)); 

我具有其中具有值static,project variable,external,output variable第三列中的JComboBox

假设有2行,所以当我在第一行中选择参数类型为static时,我希望在该特定行中启用该特定的cell(静态值),并禁用剩余的单元。 同样当我在第二行选择参数类型作为“输出变量”。我希望在特定行中启用该特定单元(输出变量),并禁用其余单元。

+0

基地您启用/禁用电池不阵列上的决定,但第三列的值... – MadProgrammer

+0

@MadProgrammer有什么办法来实现这一功能。我们可以在摇摆中禁用特定的CELL吗?如果是这样,请指导我,这有助于很多.. –

+0

是的,使用模型的getVaueAt方法来获取给定行的第三列的值,并根据您的规则返回一个布尔值,用于“isCelLEditable” – MadProgrammer

回答

2

如下更改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]; 
} 
相关问题