PARVEEN SULTHANA
PARVEEN SULTHANA

Reputation: 1

How to disable a particular cell in JTable?

I am working with swing. I Have a JTable having 8 columns and Dynamic rows. 2nd column is non-editable which I did like this in 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.

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

Upvotes: 0

Views: 4945

Answers (1)

Pasupathi Rajamanickam
Pasupathi Rajamanickam

Reputation: 2052

Change your isCellEditable as follows.

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];
}

Upvotes: 4

Related Questions