I have a JTable which is created using 2 vectors for header and data. The JTable is being created with the data. Now I want to add a new column at the end of the table to select some specific rows.
//set header
Vector header = new Vector();
for(int i=1; i<=rsmd.getColumnCount(); i++) {
header.addElement(rsmd.getColumnLabel(i));
}
header.addElement("Select");
//set data
Vector data = new Vector();
while(rs.next()) {
Vector row = new Vector();
for(int i=1; i<=rsmd.getColumnCount(); i++) {
row.addElement(rs.getObject(i));
}
row.addElement(false);
data.addElement(row);
}
DefaultTableModel dtm = new DefaultTableModel(data, header);
tbl.setModel(dtm);
This is my code for creating the table. I have added a new column called Select. Just want to fill the column with checkboxes to select one or more rows.
The easiest way to override getColumnClass(int col)
method of TableModel
like next:
DefaultTableModel dtm = new DefaultTableModel(data, header){
@Override
public Class<?> getColumnClass(int col) {
if(col == CHECK_BOX_COLUMN_INDEX){
return Boolean.class;
}
return super.getColumnClass(col);
}
};
where CHECK_BOX_COLUMN_INDEX
- index of column with JCheckBox
.
Also read Concepts: Editors and Renderers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.