【发布时间】:2017-11-20 19:36:29
【问题描述】:
我创建了一个从 Mysql 数据库中检索数据并将其显示在 JTable 中的应用程序。然后我在第一列中添加了复选框,我可以使用 TableCellRendere 显示它们。但是当我尝试检查它们时,未选中复选框。其实我在这个链接里读到了如何正确使用TableCellEditor,但是我不是很明白:
https://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableCellEditor.html
然后我得到了这段代码,但我不知道在方法public Component getTableCellEditorComponent()中添加什么。
这是我需要完成的代码:
public class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
protected JCheckBox checkBox;
public CheckBoxCellEditor() {
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
}
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
// What should I add here and can you explain me
return checkBox;
}
public Object getCellEditorValue() {
return Boolean.valueOf(checkBox.isSelected());
}
}
谢谢
【问题讨论】: