【问题标题】:How can I make a checkbox on JTable not editable after a checkbox is clicked?单击复选框后,如何使 JTable 上的复选框不可编辑?
【发布时间】:2020-11-22 08:59:22
【问题描述】:

我创建了一个有 4 列的 JTable,最后两列是 JCheckBoxes。单击第二列上的复选框后,我想禁用第三列中的复选框,反之亦然。

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

      }

      }

这是我到目前为止所做的,但第 2 列和第 3 列都是可编辑的。 请帮忙。谢谢。

【问题讨论】:

  • 这不会编译,也不是Minimal Reproducible example。请更新您的问题。
  • 到目前为止你尝试了什么?你到底有什么问题?
  • 我的问题是如何在单击 jtable 上第 3 列上的复选框后禁用第 4 列上的复选框?
  • 你应该更新你的问题。到目前为止,您尝试了什么?
  • 注意到并且我已经更新了我的问题。到目前为止我所做的,但第 2 列和第 3 列都是可编辑的。

标签: java swing jtable jcheckbox


【解决方案1】:

但第 2 列和第 3 列都是可编辑的。

嗯,这就是您的 isCellEditable(...) 方法所声明的。

如果您不希望第 3 列可编辑,那么您需要修改您的代码。可能是这样的:

case 3:
    Boolean column2 = (Boolean)getValueAt(row, 2)
    return ! column2.booleanValue();

【讨论】:

  • isCellEditableJCheckBox 被选中/取消选中时被调用。因为他想禁用邻居复选框,所以这不起作用,因为rowcol 指的是点击的复选框..
  • @MoritzSchmidt 他想禁用邻居复选框, - 我知道,这就是第 3 列的逻辑检查第 2 列状态的原因。
猜你喜欢
  • 2015-03-17
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 2014-12-17
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多