Francislainy Campos Francislainy Campos - 1 month ago 9
MySQL Question

How to retrieve data when JTable has checkbox column on it?

Many thanks for your help. :)
I am trying to add a checkbox column into a JTable connected to a database. However, I think I am missing something because the funny thing is that both things work (The checkbox and the data extraction from the database), but not when they are together! Please let me explain what I have done so far:
First thing I did was to override this class on my default table model:

data = new Object[100][10];
defaultModel = new DefaultTableModel(data, columnNames) {

@Override
public Class<?> getColumnClass(int col) {
switch(col) {
case 0:
return Integer.class;
case 1:
return Integer.class;
case 2:
return Date.class;
case 3:
return String.class;
case 4:
return String.class;
case 5:
return Boolean.class;
default:
return null;
}
}


};

table = new JTable(defaultModel);
js = new JScrollPane(table);
paneLeft.add(js, BorderLayout.CENTER);


And that is how I am trying to populate my table with the checkbox:

/* My global variables for this part of the code:
String query;
Statement stmt;
ResultSet rs;
DefaultTableModel defaultModel;
String[] columnNames = {"id","pat_id","date","time", "note", "status"};
*/



public void placeContentIntoRows(){
stmt = null;
rs = null;
query = "select * from messages";

try {
stmt = TableConnection.dbConnector().createStatement();
if (stmt.execute(query)) {
rs = stmt.getResultSet();
}

int rowCounter = 0;

while(rs.next()){
data[rowCounter][0] = rs.getInt(1
data[rowCounter][1] = rs.getInt(2);
data[rowCounter][2] = rs.getDate("3"); //When I add the right name for all the columns, column status loses its checkbox.
data[rowCounter][3] = rs.getString(4);
data[rowCounter][4] = rs.getString(5);
data[rowCounter][5] = rs.getBoolean(6);

rowCounter++;
} defaultModel = new DefaultTableModel(data, columnNames);
table.setModel(defaultModel);

} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}

table.repaint();
}


You may be able to see that I have this piece of code for column number 3:
datarowCounter = rs.getDate("3");
The quotes were added intentionally so that I can explain my problem more fully. When the quotes are there, which means I have a mistake in that part of the code, my table is displayed like that: (With no data, but with the checkbox on it).

CheckBox Displayed

https://i.stack.imgur.com/Ip68h.png

However, if I fix that part of the code and take out the quotes, my table is displayed correctly but without checkboxes!

while(rs.next()){
data[rowCounter][0] = rs.getInt(1);
data[rowCounter][1] = rs.getInt(2);
data[rowCounter][2] = rs.getDate(3); //When I add the right name for all the columns (id), column status loses its checkbox.
data[rowCounter][3] = rs.getString(4);
data[rowCounter][4] = rs.getString(5);
data[rowCounter][5] = rs.getBoolean(6);

rowCounter++;
}


This is my database table:


  • id int, pat_id int, date date, time varchar(25), note varchar(25), status
    tinyint(1)



Please do not mind the fact the variable time is set as varchar. Just did it that way for now to tackle a problem at a time. Will change it as soon as I get this checkbox to appear on the screen. 

If anyone could please have a look at my code and try to shed some light on what I am missing that would be much appreciated indeed. That is the first time I work with JTables, so it may be something simple, I know. Just can`t find it myself at this moment.
Thank you so very much for your kind help. :)

PS: That`s my table:
Field | Type | Null | Key | Default | Extra
------------+---------------+-------------+---------+-------+
Id | int (11) | NO | Pri | NULL | auto_increment
Pat_id | int (11) | NO | NULL
Date | date | NO | NULL
time | varchar(25) | YES | NULL
note |varchar(25) | YES | NULL
status |tinyint(1) | YES | NULL


Data with no checkbox

https://i.stack.imgur.com/ms5Mn.png

Method where the problem is being noticed refactored:

private void placeContentIntoTable() {
PreparedStatement st = null;
ResultSet rst = null;
try{
String query="SELECT * from messages";
st = TableConnection.dbConnector().prepareStatement(query);
if (st.execute()) rst = st.getResultSet();

int rowCounter = 0;
data = new Object[1000][10];
//String[] columnNames = {"id","pat_id", "date", "time", "note", "status"};
while(rst.next()){
for(int i=0; i<columnNames.length; i++){
data[rowCounter][i] = rst.getObject(i+1);
}
rowCounter++;
}

defaultModel = new DefaultTableModel(data, columnNames);//When it is uncommented we see data but no checkbox, when it is commented out we see the checkboxes but no data
table.setModel(defaultModel);

} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}

table.repaint();
}

Answer

Your code appears to be mess up because you have two places where you attempt to create a DefaultTableModel. Once with a custom getColumnClass(...) method and the other without.

defaultModel = new DefaultTableModel(data, columnNames);

You are just using the standard implementation of the DefaultTableModel. The above code does not override the getColumnClass(...) method so the default renderer is used which just invokes the toString() method of the Object in the column.