A tree whose nodes may be checked (e.g. the widget usually found in software installers, that allows to select which features to install/uninstall). If a node has some child of different checking status is greyed. You can use the same constructors of JTree to instantiate a new CheckboxTree Example from a TreeNode:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); root.add(new DefaultMutableTreeNode("child A")); root.add(new DefaultMutableTreeNode("child B")); CheckboxTree CheckboxTree = new CheckboxTree(root);
Example from a TreeModel:
DefaultTreeModel dtm = new DefaultTreeModel(root); CheckboxTree CheckboxTree = new CheckboxTree(root);
Default constructor (useful for gui builders):
CheckboxTree CheckboxTree = new CheckboxTree();
Then you can set the checking propagation style:
CheckboxTree.getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.SIMPLE); CheckboxTree.getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.PROPAGATE); CheckboxTree.getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.PROPAGATE_PRESERVING_CHECK); CheckboxTree.getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.PROPAGATE_PRESERVING_UNCHECK);
You can also set the model at a later time using:
CheckboxTree.setModel(aTreeModel);
There are two methods that return the paths that are in the checking set:
TreePath[] tp = CheckboxTree.getCheckingPaths(); TreePath[] tp = CheckboxTree.getCheckingRoots();
You can also add/remove a listener of a TreeCheckingEvent in this way:
CheckboxTree.addTreeCheckingListener(new TreeCheckingListener() { public void valueChanged(TreeCheckingEvent e) { System.out.println("Checked paths changed: user clicked on " + (e.getLeadingPath().getLastPathComponent())); } });
@author Enrico Boldrini
@author Lorenzo Bigagli