java JTable - 显示为复选框的布尔值并且必须是可编辑的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13303012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTable - Boolean to show as check box and must be editable
提问by user1808348
Ok so I have a JTable that I populated from an LinkedHashSet of Books.
好的,所以我有一个从 LinkedHashSet of Books 填充的 JTable。
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.
我的问题是我希望第 4 个 coloum 将布尔状态显示为复选框,并且我希望在将状态保存回 LinkedHashSet 并刷新表后能够对其进行更改。
Sorry I am rather beginner, if you can give me some advice it will be appreciated.
抱歉,我是初学者,如果您能给我一些建议,将不胜感激。
回答by tenorsax
In the table model, in getColumnClass()
return Boolean.class
for the particular column. For example for AbstractTableModel
or DefaultTableModel
extensions:
在表模型中,对于特定列的getColumnClass()
回报Boolean.class
。例如对于AbstractTableModel
或DefaultTableModel
扩展:
@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:
此外,要使单元格可编辑,请覆盖isCellEditable()
,例如:
@Override
public boolean isCellEditable(int row, int col) {
return (col == 3);
}
For more details about table models check out How to Use Tablestutorial. In the same tutorial there is an example of a table with a checkbox column.
有关表模型的更多详细信息,请查看如何使用表教程。在同一个教程中,有一个带有复选框列的表格示例。