JXTreeTable with checkbox

2

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.

2 thoughts on “JXTreeTable with checkbox

  1. jkf says:

    hej huionn,

    Im in the same situation. Trying to find/adapt different ways of implementing an checkbox in my jxtreetable. I tried your solution but didn’t get it to work. Do you maybe have example program that you could share? Thanks

  2. huionn says:

    I updated the post for your question. Actually by return Boolean.class in getColumnClass method, JXTreeTable will use checkbox to render the cell automatically.

Leave a comment