【问题标题】:How to use TableCellEditor to edit checkbox如何使用 TableCellEditor 编辑复选框
【发布时间】: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());
    }

}

谢谢

【问题讨论】:

    标签: checkbox tablecelleditor


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 2017-04-17
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    相关资源
    最近更新 更多