Skip to content
Advertisement

How can I make a checkbox on JTable not editable after a checkbox is clicked?

I have created a JTable with 4 columns, the last two columns are JCheckBoxes. I would like to disable the checkbox in column three after I clicked the checkbox on column two and vice versa.

 public class TableTest {

  public static void main(String[] args) {
    new TableTest();
    }

   public TableTest() {
     startUI();
   }

  public void startUI() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
        
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | 
     IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
    }

    MyTableModel model = new MyTableModel();
     try {  
                        Class.forName("com.mysql.jdbc.Driver");
                        String url = "jdbc:mysql://localhost/survey";
                        Connection conn = 
       DriverManager.getConnection(url,"root","");
                        Statement stat = conn.createStatement();

                        ResultSet rslt=stat.executeQuery("SELECT * FROM 
          questions_edit");
                        while(rslt.next())
                {
                         String d = rslt.getString("question_no.");
                         String e = rslt.getString("question");
                         
                         model.addRow(new Object[]{d,e, false, false});
          }
                         
        
        }catch(SQLException e){
                            e.printStackTrace();
                    }catch(ClassNotFoundException e){
                            e.printStackTrace();
                } 
     JTable table = new JTable();
                table.setModel(model);

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(table));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
       }
         });
      }

           public class MyTableModel extends DefaultTableModel {

          public MyTableModel() {
       super(new String[]{"Question No.", "Question", "Satisfied", "Not 
           Satisfied"}, 0);
           }

           @Override
           public Class<?> getColumnClass(int columnIndex) {
             Class cls = String.class;
        switch (columnIndex) {
              case 0:
            cls = Integer.class;
          break;
          case 2:
       cls = Boolean.class;
       break;
       case 3:
      cls = Boolean.class;
      break;
      }
       return cls;
         }

@Override
public boolean isCellEditable(int row, int column) {
  switch (column) {
     case 2: return true;
    
     case 3: return true;
           
     
 }
 return false;
}

@Override
public void setValueAt(Object aValue, int row, int column) {
  if (aValue instanceof Boolean && column == 2) {
    System.out.println(aValue);
    Vector rowData = (Vector)getDataVector().get(row);
    rowData.set(2, (boolean)aValue);
    fireTableCellUpdated(row, column);
  }
  else if (aValue instanceof Boolean && column == 3) {
    System.out.println(aValue);
    Vector rowData = (Vector)getDataVector().get(row);
    rowData.set(3, (boolean)aValue);
    fireTableCellUpdated(row, column);
      
      }
       }

      }

      }

This is what I’ve done so far but both column 2 and 3 are editable. Please help. Thank you.

Advertisement

Answer

but both column 2 and 3 are editable.

Well, that is what your isCellEditable(...) method states.

If you don’t want column 3 to be editable then you need to modify your code. Maybe something like:

case 3:
    Boolean column2 = (Boolean)getValueAt(row, 2)
    return ! column2.booleanValue();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement