Ok so I have a JTable that I populated from an LinkedHashSet of Books.
public static void LibToArray(){
rowData = new Object[Book.bookList.size()][5];
int i = 0;
Iterator it = Book.bookList.iterator();
while(it.hasNext()){
Book book1 = (Book)it.next();
rowData[i][0] = (Integer)book1.getId();
rowData[i][1] = book1.getTitle();
rowData[i][2] = book1.getAuthor();
rowData[i][3] = (Boolean)book1.getIsRead();
rowData[i][4] = book1.getDateStamp();
i++;
}
}
My issue Is I want the 4th coloum to show the Boolean status as a check Box, and I want it to be able to be changed, after saving the status back to the LinkedHashSet and refreshing the table.
Sorry I am rather beginner, if you can give me some advice it will be appreciated.
In the table model, in getColumnClass()
return Boolean.class
for the particular column. For example for AbstractTableModel
or DefaultTableModel
extensions:
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 3)
return Boolean.class;
return super.getColumnClass(columnIndex);
}
Also, to make the cell editable, override isCellEditable()
, for example:
@Override
public boolean isCellEditable(int row, int col) {
return (col == 3);
}
For more details about table models check out How to Use Tables tutorial. In the same tutorial there is an example of a table with a checkbox column.