JTree with checkboxes
Asked Answered
D

4

13

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)

Dominicdominica answered 3/8, 2009 at 16:6 Comment(0)
P
8

As for #2, you could make a panel be the editor/renderer, and add a label along with the checkbox - the label would be the text, and the check box would not have the text added to it.

Paranoiac answered 3/8, 2009 at 17:0 Comment(1)
Just be aware that if you just copy the DefaultTreeCellRenderer code and replace the extension of JLabel with a JPanel, you'll also have to remove a lot of the "overridden for performance" methods or you'll have a blank JTree.Imbecility
W
9

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.

Wham answered 20/2, 2012 at 12:16 Comment(1)
JIDE Common Layer brokes all your collor scheme because it installs its own UI, didn't find up how to fix, if someone know how please share it.Anabolism
P
8

As for #2, you could make a panel be the editor/renderer, and add a label along with the checkbox - the label would be the text, and the check box would not have the text added to it.

Paranoiac answered 3/8, 2009 at 17:0 Comment(1)
Just be aware that if you just copy the DefaultTreeCellRenderer code and replace the extension of JLabel with a JPanel, you'll also have to remove a lot of the "overridden for performance" methods or you'll have a blank JTree.Imbecility
D
4

per @aperkins suggestion this is what I ended up doing in the TableCellRenderer, it seems to work well:

final private JPanel nodeRenderer = new JPanel();
final private JLabel label = new JLabel();
final private JCheckBox check = new JCheckBox();

     ...

// in constructor:
final Insets inset0=new Insets(0,0,0,0);        
this.check.setMargin(inset0);
this.nodeRenderer.setLayout(new BorderLayout()); 
this.nodeRenderer.add(this.check, BorderLayout.WEST);
this.nodeRenderer.add(this.label, BorderLayout.CENTER);

The keys for getting rid of unwanted space in the margins seems to be (a) calling JCheckBox.setMargin() to reduce the checkbox margin, and (b) using a BorderLayout for JPanel.

Dominicdominica answered 3/8, 2009 at 19:35 Comment(0)
A
3

I have released a standalone Swing Checkbox Tree project, available from Maven Central as org.scijava:swing-checkbox-tree.

The package is based on John Zukowski's CheckBox Node Tree Sample code. It is BSD-2-licensed with no dependencies.

It allows mixing and matching of DefaultMutableTreeNode and CheckBoxNodeData node types. It also allows check box nodes as non-leaf nodes.

Regarding the question's issue #1: I did not test on all platforms, but on my OS X systems, the CheckBoxNodeData nodes are exactly the same height in pixels as the DefaultMutableTreeNode nodes.

Regarding the question's issue #2: it uses a JCheckBox + JLabel in a JPanel (as aperkins suggests) to differentiate between clicking on a check box (to check/uncheck a node) versus a label (to select a node).

Example usages:

The latter example also has code for recursively toggling subtrees in response to boxes being checked or unchecked (see the treeNodesChanged method).

Assumpsit answered 12/10, 2012 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.