Search code examples
javaswingjtablejcheckbox

Add jcheckbox in jtable


I am currently working on an attendance system where I'll use jTable and add jCheckbox in it. However, I have no idea how to do this.

What should I do to add jCheckBox in my jTable. The data in my jTable is acquired from a database.

I have tried using this code but the table doesn't show the data from my database and still doesn't have a checkbox on it.

public void Update_table(int Column, int ColumnBoolean, DefaultTableModel model) {
    try {
        String sql = "select * from student_info";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        Attendance.setModel(DbUtils.resultSetToTableModel(rs));
        Object[] files = new Object[Column];
        while (rs.next()) {
            for (int i = 1; i <= Column; i++) {
                if (i == ColumnBoolean) {
                    files[ColumnBoolean - 1] = Boolean.FALSE;
                } else {
                    files[i - 1] = rs.getObject(i - 1);
                }
                model.addRow(files);
            }
            Attendance.updateUI();
            rs.close();
        }
        JCheckBox check = new JCheckBox();
        Attendance.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(check));
        Attendance.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer());
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

Solution

  • First of all you have all kinds of issues with the posted code:

    1. Variable names should NOT start with an upper case character. Some are correct, other are not. Be consistent and follow Java conventions

    2. Method names should NOT start with an upper case character. Again follow Java conventions.

    3. Why are you passing a "DefaultTableModel" as a parameter to the method. You never use that variable. Get rid of the parameter if it is not needed!

    4. There is no need to invoke updateUI(). The method is invoked internally on a Look and Feel change. The JTable will repaint itself when the model is added to the table.

    5. Did you debug your while loop? I don't believe it will work. When using the DBUtils it will loop through the ResultSet to add the data to the TableModel. So when you execute that code you are already at the end of the ResultSet.

    6. In any case I don't understand what you are attempting to do with the loop. All the data has already been added to the TableModel. I don't know why you would attempt to add more rows.

    but the table doesn't show the data from my database

    Well that is your first step. Display the data from the database and forget about the custom renderer/editor. Solve one problem at a time.

    Once you get the data displayed, there is still no reason to use a custom renderer/editor because Swing already provides defaults. The problem is that you need to override the getColumnClass(...) method of your JTable to return the Class of each column so the table can choose the apppriate renderer/editor for the column.

    For an example of how to do this see: Java, changing boolean column to checkbox in jTable when using rs2xml for populating jTable