December 30, 2011 by huionn
I need to have checkbox column in JXTreeTable. I google and found that the solution is quite troublesome (the solutions were dated a few years ago).
So I tried to check JXTreeTableVisualCheck class. I found that its interactiveEditWithComboBox() show a convenient use of DefaultCellEditor(…).
After some testing, I found that in latest version of SwingX, checkbox in JXTreeTable can be done with relatively little effort.
TableColumn selCol = treeTable.getColumnModel().getColumn(4); JCheckBox cb = new JCheckBox(); cb.setHorizontalAlignment(SwingConstants.CENTER); // without this, checkbox move to left during click selCol.setCellEditor(new DefaultCellEditor(cb));
As I found out later, JXTreeTable has built-in support for checkbox. Just need to return Boolean.class in public Class<?> getColumnClass(int column) method. There is no need to explicitly set cell editor as shown above for boolean value.
JXTreeTablePanel.java is a simple example for editable checkbox in JXTreeTable.