1

I have data from the database on my 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. A sample code could help me understand how to do this.

Code below is for updating my 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);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Elated Coder
  • 312
  • 1
  • 16
  • 1
    Why not set the table so multiple rows can be selected, and have a separate (outside the table) button to `Delete Selected Rows`? – Andrew Thompson Jul 10 '16 at 13:16
  • 1
    I don't want the user to have hard time in trying to remember something like ctrl+a or ctrl+c .i just want them to use checkboxes that are more user friendly to them – Elated Coder Jul 10 '16 at 13:22
  • I completely disagree. But hey, good luck with it. – Andrew Thompson Jul 10 '16 at 13:25
  • 1
    In this [question](http://stackoverflow.com/questions/17225988/how-to-add-jbutton-after-a-jtable) I had done something similar (it didn't came from a database but you can change the data object to your result set of your select query) – Frakcool Jul 10 '16 at 13:43
  • 1
    Myself, I'd create my own table model from the database data, and add a column of Booleans to the data. – Hovercraft Full Of Eels Jul 10 '16 at 13:53
  • @HovercraftFullOfEels please elaborate – Elated Coder Jul 10 '16 at 13:59
  • Wannabe: have a look at the Oracle tutorial on JTables, especially the model section, and then create your own table model, not using DbUtils. – Hovercraft Full Of Eels Jul 10 '16 at 14:01
  • 1
    @WannabeJavaGeek *please elaborate* Please elaborate a [mcve] for us to help you, as is, this question cannot have more help than the link and recommendation comments we're giving you – Frakcool Jul 10 '16 at 14:08
  • Start with this complete [example](http://stackoverflow.com/a/34742409/230513). – trashgod Jul 10 '16 at 15:31

2 Answers2

2

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.

  • The default renderer uses a check box when getColumnClass() returns Boolean.class for Row.checked.

  • In the corresponding JDBC and SQL, the checked attribute is of type boolean.

image

$ 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()));
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

The problem is DBUtils returns a complete TableModel.

The easiest way is to NOT use DBUtils and to load the data from the ResultSet into the TableModel yourself.

The code is not difficult and you can use the Table From Database Example found in Table From Database as the starting point.

The code just loads the data from the ResultSet into Vectors, so you can then manually add another column to contain Boolean data.

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 );
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
camickr
  • 321,443
  • 19
  • 166
  • 288