I am using CheckBoxTree in a project and it works well. Still, I have a problem with checking specific nodes after the tree was created. It should be something easy and I am pretty sure to miss something... but I am completly stuck on it and can't figure it out.

I am creating a checkBoxTree from a path to enable the user to check several paths. The path "hotfix" (if present) needs to be checked by default and the node should be disabled (the code below does not do anything to disable the checkbox, though).
What I am doing is:
- Code: Select all
// Prepare the tree
root = new File(DataRepository.getNewPath());
rootNode = new FileSystemTreeNode(root);
DefaultTreeModel myModel = new DefaultTreeModel(rootNode);
checkboxTree = new CheckBoxTree(myModel);
checkboxTree.setRootVisible(false);
checkboxTree.setShowsRootHandles(false);
checkboxTree.getCheckBoxTreeSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
checkboxTree.getCheckBoxTreeSelectionModel().setSingleEventMode(false);
checkboxTree.getCheckBoxTreeSelectionModel().setDigIn(false);
// Mark the hotfix folder to be patched by default
FileSystemTreeNode hotfixNode = null;
int c = 0;
while (c < rootNode.getChildCount()) {
FileSystemTreeNode nodeToCheck = rootNode.getChildAt(c);
if ((nodeToCheck.toString().toLowerCase()).equals("hotfix")) {
hotfixNode = nodeToCheck;
TreePath hotfixTreePath = new TreePath(hotfixNode.getPath());
checkboxTree.getCheckBoxTreeSelectionModel().addSelectionPath(hotfixTreePath);
}
c++;
}
This seems to be logical. The change handler looks like this:
- Code: Select all
checkboxTree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath[] treePaths = checkboxTree.getCheckBoxTreeSelectionModel().getSelectionPaths();
DefaultListModel selectedModel = new DefaultListModel();
filesArray = new ArrayList<File>();
filesArray.clear();
if (treePaths != null) {
mContinueAction.setEnabled(true);
for (TreePath path : treePaths) {
selectedModel.addElement(path);
filesArray.add(f);
}
} else {
mContinueAction.setEnabled(false);
}
selectedList.setModel(selectedModel);
}
});
I use a JList to display the currently selected paths (selectedList).
When I run this, the path "hotfix" is never selected. Never the less, the hotfix folder is found by my code, but the node that is added looks different to the once that are added by a click on the control.
If I click on the folder hotfix, the entry in the list looks like:
- Code: Select all
[D:\Test\Source, hotfix]
(where "D:\Test\Source" is the root node)
the one that is added by my code looks like:
- Code: Select all
[hotfix]
(obviously missing the root)
I am coming to think that the reason the checkbox is not "ticked" is because the path is wrong (missing root?). But what I did was a .getPath on the node. Shouldn't this be complete? Because the ticked path is stored in the List, the software is doing the right thing, but the tree does not reflect it...
Any hint is much appreciated! I have been fiddling with this for hours and google brought me as far as I am now. Its beginning to drive me crazy...
Using jide-oss-2.9.7.jar.
Many thanks and greetings,
Christian
EDIT:
- Code: Select all
TreePath hotfixTreePath = new TreePath(nodeToCheck.getPath());
does always return the a TreePath with only the last node, not the full path (as expected).
I have no idea what is going wrong here...
EDIT 2:
- Code: Select all
TreePath hotfixTreePath = new TreePath(nodeToCheck.getPath());
checkboxTree.getCheckBoxTreeSelectionModel().addSelectionPath(hotfixTreePath);
- Code: Select all
TreePath path = checkboxTree.getPathForRow(c);
checkboxTree.getCheckBoxTreeSelectionModel().addSelectionPath(path);
I do not understand, why the other doesn't work, but for now I am happy it works.
Maybe a bug?
So, nevermind, folks!