JTable with editable checkbox

一曲冷凌霜 提交于 2019-12-02 05:55:33

1) JTable columns indexes starts from 0, if you want to set JCheckBox to last column, you need to use index 4 , not 5 as in your code.

2)For adding action to your JCheckBox column you can use DefaultCellEditor with JCheckBox, for example:

JCheckBox chb = new JCheckBox();
chb.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent e) {
            System.out.println("action");
    }
});
DefaultCellEditor editor = new DefaultCellEditor(chb) {
    @Override
    public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row,int column) {
           Component tableCellEditorComponent = super.getTableCellEditorComponent(table, value, isSelected, row, column);
           ((JCheckBox)tableCellEditorComponent).setHorizontalAlignment(JCheckBox.CENTER);
           return tableCellEditorComponent;
    }
};
retunTable.getColumnModel().getColumn(4).setCellEditor(editor);

@user3318622 : You need to use TableCellEditor and TableCellRenderer if you want to display checkbox in the last column cell and in the TableCellEditor you need to handle ItemListener for checkbox.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!