3

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.

user1808348
  • 79
  • 1
  • 1
  • 4
  • I appreciate the answers but even with that answer I am still struggling, I have looked through the table tutorials as well . Can someone please give me a more in depth explanation please :) – user1808348 Nov 09 '12 at 12:23
  • I cited an example [here](http://stackoverflow.com/a/13301413/230513). – trashgod Nov 09 '12 at 13:31

1 Answers1

8

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.

tenorsax
  • 21,123
  • 9
  • 60
  • 107