I'm trying to create a Jtable that takes displays SQL queries. I have inserted a column before the data is shows and I have set it to the Boolean class. When I run the code instead of showing checkboxes, it displays 'class java.lang.Boolean' instead.
Ive tried calling the isCellEditable() and getColumnClass() methods.
public static void FillTable(JTable table, String Query){
try
{
Connection conn = DriverManager.getConnection(databaseTest.main_conn,"root",databaseTest.db_usrPW);
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(Query);
//Define new model
DefaultTableModel tableModel = new DefaultTableModel();
//Get MetaData from rs
ResultSetMetaData metaData = rs.getMetaData();
//Get number of columns from meta data
int columnCount = (metaData.getColumnCount()+1);
//Add Boolean column
tableModel.addColumn(Boolean.class);
tableModel.getColumnClass(0);
//Get all column names from meta data and add columns to table model
for (int columnIndex = 1; columnIndex < columnCount; columnIndex++){
tableModel.addColumn(metaData.getColumnLabel(columnIndex));
}
//Create array of Objects with size of column count from meta data
Object[] row = new Object[columnCount];
//Scroll through result set
while (rs.next()){
row[0] = Boolean.class;
tableModel.addRow(row);
//Get object from column with specific index of result set to array of objects
for (int i = 1; i < columnCount; i++){
row[i] = rs.getObject(i);
}
//Now add row to table model with that array of objects as an argument
tableModel.addRow(row);
}
db_table.setModel(tableModel);
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}