Reputation: 489
I have a problem. I create a DefaultTableModel like this -->
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>(1,1);
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>(10,2);
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
The ResulSet is filled with some Data of a MySQL Database. After that I create a JTable with the DataModel and override set the tableproperties:
this.table = new JTable(buildTableModel(this.rs)){
// Make Cells uneditable
@Override
public boolean isCellEditable(int row, int column) {
if(table.getColumnName(column).contains("key")) return false;
if(table.getColumnName(column).contains("datum")) return false;
if(table.getColumnName(column).contains("user")) return false;
return true;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
Class<?> classType;
if(columnIndex == 1)
classType = Boolean.class;
else
classType = String.class;
return classType;
}
};
If I don't use the second part (@Override getColumClass), it works fine, but if I add the getColumnClass part, I get this errormessage:
java.lang.String cannot be cast to java.lang.Boolean
The field contains "true" / "false". I'm doing something pretty wrong, but I have really no idea what exactly ;-( Can someone help me out with that?
Thanks a lot
Edit: I changed the buildTableModel to this:
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>(1,1);
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>(10,2);
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
if(rs.getObject(columnIndex).toString().equals("true"))
vector.add(Boolean.TRUE);
else
if(rs.getObject(columnIndex).toString().equals("false"))
vector.add(Boolean.FALSE);
else
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
and now it works ;-)
Upvotes: 0
Views: 1041
Reputation: 109823
metaData.getColumnType can returns (note very simple matrix and hardcoded with minimum effort)
public Class getColumnClass(int column) {
int type;
try {
type = metaData.getColumnType(column + 1);
} catch (SQLException e) {
return super.getColumnClass(column);
}
switch (type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class;
case Types.BIT:
return Boolean.class;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class;
case Types.BIGINT:
return Long.class;
case Types.FLOAT:
case Types.DOUBLE:
return Double.class;
case Types.DATE:
return java.sql.Date.class;
default:
return Object.class;
}
}
Upvotes: 2
Reputation: 2121
You're trying to assign a String value to a Boolean column (in your Swing table). Just make sure that the types of the data (the data model) retrieved match those of the table data model.
Upvotes: 1