Monday, November 27, 2006

Tree (and tree table) with checkboxes

When the user needs to select a non-trivial number of items from a hierarchy, normal tree item selection is too prone to losing the selection through mistaken clicks. The standard solution to provide a more robust selection method is to include check boxes next to each selectable item in the tree.

Hat tip to some previous online implementations (each has a different implementation):

Bare bones

With a TreeCellEditor

More recent variations

After a couple of iterations of checkbox trees (and tree tables), I rolled this up into a standalone TreeCellRenderer so that it can be applied more easily to any tree-oriented component. This implementation also properly handles rollover effects on the checkbox, which is lacking in the above implementations (most visible under w32; Java's Metal LAF has no checkbox rollover effects).

The checkbox painting is handled by using a real checkbox and painting its contents into an icon. The state of the checkbox is set just prior to painting to ensure the proper rollover state. The original cell renderer's icon is replaced with a composite icon of the original plus the checkbox (hit ^-shift-O to switch component orientation).

The renderer provides the current checked selection as an array of TreePath or an array of int representing the rows.

I also applied this to my own tree table implementation (similar to SwingX JXTreeTable) by just overriding a few protected methods which translate mouse coordinates into rows/paths.



Santhosh Kumar T said...


I have also posted similar stuff in my weblog long back;

JTree with CheckBoxes:

CheckTree with/without Selection Digging

Show CheckBoxes only on specific Tree Nodes

JXTreeTable with CheckBoxes

sai said...

Thank u sir for what your doing it is helping a lot to improve our coding skills

thank u

anjana said...

What is the License under which we can use this code.

technomage said...

This code is licensed under LGPL. It will shortly be available in code repository at

anjana said...

Thanks for your reply. I tried your renderer and it works great. thanks. But I will not be able to use it unless the code has the LGPL license comment in it. So do you have any idea when the code will be posted in the website you metioned with the license comment ?

Thanks again.

technomage said...

Should be done within a week.

manunu said...

oh my, i've been searching forever for such a piece of code. thx so much!!

best wishes, manunu

Lostprophet said...

Hi technomage,

first of all I'd like to thank you for this post, it is just what I've been looking for (for quite a while).

But I also have a request:
Would you be so kind to provide a jar-file containing the updated version of the source including the license comment? Also, please do not use the default package as you have done in the first version of your jar.

See, I want to use your renderer in a commercial project and can only do so without touching my own license if I import your renderer as a library (hence the need for a jar).
In addition, since Java 1.5 it is not possible anymore to access classes that are in the default package from a named package. And that is why I can't use your older jar (apart from the missing license comment).

So I need your renderer in a jar with the license comment in a named package, can you do that please?

Best Regards,


technomage said...

You have permission to use the code under the BSD license if you wish.

Lostprophet said...

That's great, thank you very much!