Geertjan's Blog

  • September 28, 2015

Programmatically Selecting a Node

Geertjan Wielenga
Product Manager

When my application starts up, the Node hierarchy is automatically expanded and the selected Node you see below is automatically selected:

Here's how:

try {
em.setSelectedNodes(new Node[]{
} catch (PropertyVetoException ex) {

You'd probably want some check to make sure the Node exists, is not Null, etc. Another factor to consider is that you may need to add a thread to wait for the Nodes to be created before selecting the one you'd like to have selected.



Join the discussion

Comments ( 2 )
  • Jose Renteria Wednesday, October 7, 2015

    Hello Geertjan, thank you very much for your post. On key element to select nodes I noticed in your code is to use false when creating the children:

    Children movieChildren = Children.create(new MovieChildFactory(), false);

    This way the tree is generated in the same thread and selection can happen.

    Again, thank you very much !

  • jmborer Thursday, October 8, 2015

    And here an example to expand nodes that you may have stored before. For example when you want to restore a tree expansion state after a restart of your app.


    * Expand a path for matching names.


    * WARNING: if nodes are created lazily, the path must be expanded element by element.

    * If for example path is {"conf", "gva", "services"}, you must call this method:

    * <pre>

    * tree.expandPath({"conf"})

    * tree.expandPath({"con", "gva"})

    * tree.expandPath({"con", "gva", "services"})

    * </pre>

    * @param path


    public void expandPath(String[] path) {

    if (path != null) {

    ExplorerManager em = ExplorerManager.find(this);

    try {

    Node n = NodeOp.findPath(em.getRootContext(), path);


    } catch (NodeNotFoundException e) {




    And here you can store the state:

    public List<String[]> getExpandedPaths() {

    List<String[]> result = new ArrayList<>();

    // Look for explorer manager backing this treeview

    ExplorerManager em = ExplorerManager.find(this);

    TreeNode treeRootNode = Visualizer.findVisualizer(em.getRootContext());

    TreePath rootNodeTreePath = new TreePath(treeRootNode);

    Enumeration<TreePath> exPaths = tree.getExpandedDescendants(rootNodeTreePath);

    if (exPaths == null) {

    return result;


    while (exPaths.hasMoreElements()) {

    TreePath etp = exPaths.nextElement();

    Node node = Visualizer.findNode(etp.getLastPathComponent());

    String[] path = NodeOp.createPath(node, em.getRootContext());



    return result;


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.