There needs to be more sleep hours in the day. I could have used a bit more sleep this morning let’s face it. But that’s not going to happen now. Oh great I have hiccups now. I don’t know where that came from. Let’s see if I can hold my breath. If that helps then the hiccups will be gone and everything will be fine and dandy. Nope that’s not happening either. Dang it. Maybe I’ll get there maybe.
Every once in a while we need to populate data in a JTable. Here's a simple to use DefaultTableModel for populating a JTable from a ResultSet.
public DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
Vector columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
return dtm;
}
Comments
Post a Comment