0

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);
    }
}
TT.
  • 15,774
  • 6
  • 47
  • 88
  • I actually can't understand anything about that. I don't know what is the function of "vector" and other stuff. – Sydney Delavin Nov 10 '19 at 12:19
  • There's a lot of your program that we're not seeing. You should consider [edit]ing your question and including a [short, self contained, correct (compilable), example](http://www.sscce.org/). That way it is easier for readers here to help you out. – TT. Nov 10 '19 at 12:30

1 Answers1

1

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

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you sir, I managed to display the contents of my database into the table. The only result I want is to add column in the table where there is a checkbox inside of it. – Sydney Delavin Nov 11 '19 at 04:45
  • @SydneyDelavin, To add a column of Boolean data that is not found in the database you can check out: https://stackoverflow.com/questions/32445418/how-to-add-checkbox-in-jtable-populated-using-rs2xml/32446003#32446003 – camickr Nov 11 '19 at 15:16
  • Thank you for that information sir. But I just want to ask a question, the code TableModel utilsModel = DbUtils.resultSetToTableModel(rs); TableModel wrapperModel = new CheckBoxWrapperTableModel(utilsModel),"Select"); Attendance.setModel(wrapperModel); changes every column name on my table to "Select". How could I fix this? – Sydney Delavin Nov 14 '19 at 17:39
  • @SydneyDelavin, I updated the code in the link to provide a basic example for testing. Frist test that code to make sure it works and then update your copy of the model from the link (in case I changed anything). If it doesn't work for you I don't know what the problem would be. – camickr Nov 14 '19 at 19:53