Return names of selected items from a checkboxTree

1

How can I check which items are selected from a checkboxTree ?

I've had to search and I know this code returns the last selected:

DefaultMutableTreeNode node = (DefaultMutableTreeNode) checkboxTree1.getLastSelectedPathComponent();

Is there any way to know the nodes names of the selected checkboxes?

Inthiscasesomethingthatreturnsme"Colors, blue, red".

    
asked by anonymous 04.12.2014 / 18:44

1 answer

1

node has a method getUserObject which returns the object relative to the current node (eg, if you fed the tree with the string "red" just convert the Object to a String ):

String color = (String) node.getUserObject();

To search for all selected paths you can use:

TreePath[] paths = checkboxTree1.getSelectionPaths();

In turn each TreePath also has method getLastPathComponent() :

if (paths != null) {
    for (TreePath path : paths) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        System.out.println(node.getUserObject());
    }
}

If you also want relatives (I do not know if that's the case) you can still use the getPath that returns a Object[] with all elements from the root.

    
04.12.2014 / 21:38