如何检查jTable的勾选或不勾选复选框

时间:2014-04-29 14:24:49

标签: java swing checkbox jtable

我需要检查是否选中了复选框,以便删除或修改相应的行。 我使用的动态jTable在每次插入时都会发生变化。

  DefaultTableModel model = (DefaultTableModel)tab1.getModel();
  model.setRowCount(0);
  try{
      Class.forName("java.sql.Driver");
      Connection conn =      (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/radees","root","123");
      Statement st = conn.createStatement();
      String query="SELECT * FROM stage ORDER BY Nom,ecole ";
      ResultSet rs = st.executeQuery(query);
      while(rs.next()){
          String d1 = rs.getString("Nom");
          String d2 = rs.getString("Prenom");
          String d3 = rs.getString("CIN");
          String d4 = rs.getString("Service");
          String d5 = rs.getString("debut");
          String d6 =rs.getString("fin");
          String d7 =rs.getString("ecole");
          model.addRow(new Object[] {d1,d2,d3,d4,d5,d6,d7});


      }

      rs.close();
      st.close();
      conn.close();
  }



    catch(Exception e){
        JOptionPane.showMessageDialog(this,"Erreur de Connection!");
    }
  jLabel9.setText(tab1.getModel().getValueAt(1,6).toString());

2 个答案:

答案 0 :(得分:2)

只需调用方法

即可

public Object getValueAt(int row, int col)

表格模型的

,其中rowcol表示您的 复选框是。它应该返回一个Boolean对象 (布尔值的默认呈现是复选框)。

答案 1 :(得分:0)

如果选中,则检查以下代码

          DailydatasTbl.getModel().addTableModelListener(new TableModelListener() {
        public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        TableModel model = (TableModel)e.getSource();
        String changedValue = model.getValueAt(row, 0).toString();
        Boolean checked=(Boolean) model.getValueAt(row, 0);

        if(checked==true){
            popupMenu = new JPopupMenu();
            menuItemAdd = new JMenuItem("Add New Row");
            menuItemRemove = new JMenuItem("Remove Current Row");
            menuItemRemoveAll = new JMenuItem("Remove All Rows");

           // menuItemAdd.addActionListener(this);
          // menuItemRemove.addActionListener(this);
          //menuItemRemoveAll.addActionListener(this);

              popupMenu.add(menuItemAdd);
              popupMenu.add(menuItemRemove);
               popupMenu.add(menuItemRemoveAll);
            popupMenu.setLocation((int)jButton1.getLocation().getX(),(int)jButton1.getLocation().getY()-10);
              popupMenu.setVisible(true);

        }else{
              popupMenu.setVisible(false);
        }

    }

});