简体   繁体   English

如何动态地将JCheckBox添加到从MySQL数据库检索到JTable的每一行

[英]How to add JCheckBox dynamically to each row retrieved from MySQL database to JTable

I have data from the database on my JTable . 我在JTable上有数据库中的数据。 I want to add checkboxes to each row dynamically from the database. 我想从数据库动态地向每行添加复选框。 I will later use these checkboxes to delete rows checked from the database. 我稍后将使用这些复选框删除从数据库中检查的行。 I know how to delete from the database, but how to add checkboxes from the database? 我知道如何从数据库中删除,但如何从数据库中添加复选框? Any one done this before please help. 任何人在此之前完成此任务请帮助。 I am a Java enthusiast trying to learn it. 我是一个尝试学习它的Java爱好者。 A sample code could help me understand how to do this. 示例代码可以帮助我了解如何执行此操作。

Code below is for updating my JTable : 下面的代码用于更新我的JTable

public void UpdateTable() {

    try {

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"
                + "employee_certificate", "root", "");

        String sql = "SELECT certificate.Number, staff.Emp_Id, staff.Emp_Name, "
                + "staff.Department,  certificate.Cert_Code, certificate.Cert_Name,\n"
                + "certificate.Vendor, certificate.Date_Taken, "
                + "certificate.Expiry_Date FROM staff LEFT JOIN certificate"
                + " ON staff.Emp_Id=certificate.Emp_Id ORDER BY staff.Emp_Name\n";

        PreparedStatement pstmt = con.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

Starting from this complete example , I made the changes below to get the following result. 从这个完整的示例开始 ,我进行了以下更改以获得以下结果。 In particular, 尤其是,

  • Row is given an attribute Boolean checked , replacing String name . Row被赋予一个Boolean checked的属性,替换了String name

  • The default renderer uses a check box when getColumnClass() returns Boolean.class for Row.checked . getColumnClass()Row.checked返回Boolean.class时,默认渲染器使用复选框。

  • In the corresponding JDBC and SQL, the checked attribute is of type boolean . 在相应的JDBC和SQL中, checked属性的类型为boolean

图片

$ diff Original.java WorkerTest.java 
48c48
<         String name;
---
>         Boolean checked;
91c91
<                     return row.name;
---
>                     return row.checked;
105a106,116
>         @Override
>         public Class<?> getColumnClass(int colIndex) {
>             switch (colIndex) {
>                 case 0:
>                     return Integer.class;
>                 case 1:
>                     return Boolean.class;
>             }
>             return null;
>         }
> 
114c125
<                         r.name = rs.getString(2);
---
>                         r.checked = rs.getBoolean(2);
138c149
<             st.execute("create table city(id integer, name varchar2)");
---
>             st.execute("create table city(id integer, checked boolean)");
143,144c154
<                 ps.setString(2, (char) ('A' + r.nextInt(26))
<                     + String.valueOf(r.nextInt(1_000_000)));
---
>                 ps.setBoolean(2, Boolean.valueOf(r.nextBoolean()));

The problem is DBUtils returns a complete TableModel . 问题是DBUtils返回一个完整的TableModel

The easiest way is to NOT use DBUtils and to load the data from the ResultSet into the TableModel yourself. 最简单的方法是不使用DBUtils并自己将ResultSet的数据加载到TableModel

The code is not difficult and you can use the Table From Database Example found in Table From Database as the starting point. 代码并不难,你可以使用Table From Database Example中发现的表从数据库为出发点。

The code just loads the data from the ResultSet into Vectors, so you can then manually add another column to contain Boolean data. 代码只是将ResultSet中的数据加载到Vectors中,因此您可以手动添加另一列以包含布尔数据。

The (two) changes to the code would be something like: 对代码的(两个)更改将类似于:

//  Get column names

for (int i = 1; i <= columns; i++)
{
    columnNames.addElement( md.getColumnName(i) );
}

columnName.addElement( "Check Mark" ); // added

//  Get row data

while (rs.next())
{
    Vector<Object> row = new Vector<Object>(columns);

    for (int i = 1; i <= columns; i++)
    {
        row.addElement( rs.getObject(i) );
    }

    row.addElement( Boolean.FALSE ); // added
    data.addElement( row );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM