JTree with checkboxes

Jason S picture Jason S · Aug 3, 2009 · Viewed 16.8k times · Source

I need to add checkboxes to a JTree. A custom TreeCellRenderer/TreeCellEditor seems like the right approach. So far I used the CheckBoxNodeRenderer approach in this webpage. It works OK except for two things:

  1. there's additional whitespace above + below the checkbox; I'd like to keep it the same as a regular JTree.
  2. I would like to distinguish between clicking on the checkbox itself (which should attempt to toggle the checkbox) and clicking on the text associated with the checkbox (which should allow an event listener to interpret this as clicking on the corresponding tree node and take whatever action is appropriate)

is there a way to do these things? I looked around for JTrees with checkboxes, can't find much. JIDE looks good but I need to use free open-source software (GPL is not ok, LGPL is ok) in this case. (or create my own checkbox tree)

Answer

metator picture metator · Feb 20, 2012

I know this question has been answered already, but i just want to clear some points:

1) JIDE Common Layer is dual-licensed (GPL with classpath exception and free commercial license). This means that you can use the Common Layer Project without any licensing issues. Please check the following link to confirm: http://www.jidesoft.com/products/oss.htm. The Common Layer includes an implementation of a checkable JTree (com.jidesoft.swing.CheckBoxTree).

2) There's a blog dated from 2005 from the Master himself, Santhosh Kumar, where he explains how to implement a JTree with checkboxes with the requirements you mentioned: http://www.jroller.com/santhosh/entry/jtree_with_checkboxes. It's worth reading it, in my opinion.