[英]How to use TableCellEditor to edit checkbox
I created an application that retrieves datas from a Mysql database and displays them in a JTable. 我创建了一个应用程序,该应用程序从Mysql数据库检索数据并将其显示在JTable中。 Then I added checkboxes in the 1st column and I am able to display them with TableCellRendere. 然后,我在第一列中添加了复选框,并能够使用TableCellRendere显示它们。 But when I try to check them , the checkboxes are not checked. 但是当我尝试检查它们时,未选中复选框。 In fact, I read how to use properly a TableCellEditor in this link, but I didn't understand well: 实际上,我在此链接中了解了如何正确使用TableCellEditor,但我不太了解:
https://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableCellEditor.html https://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableCellEditor.html
Then I got this code but I don't know what to add in the method public Component getTableCellEditorComponent()
. 然后,我得到了这段代码,但是我不知道在方法public Component getTableCellEditorComponent()
要添加什么。
Here is the code which I need to complete: 这是我需要完成的代码:
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());
}
} }
Thanks 谢谢
I have resolved the issue: 我已经解决了这个问题:
[ https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java][1] [ https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java ] [1]
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.