Print selected nodes of a Jtree

Asked

Viewed 257 times

0

I got this Jtree:

inserir a descrição da imagem aqui

I have this code that returns to me all the nodes of JTree:

public void print() {
        recurse((TreeNode) jTree2.getModel().getRoot());
    }

    public void recurse(TreeNode theNode) {
        System.out.println("1-- " + theNode);
        for (Enumeration theChildren = theNode.children(); theChildren.hasMoreElements();) {
            recurse((TreeNode) theChildren.nextElement());
        }
    }

Output of that code:

1-- JTree
1-- colors
1-- blue
1-- violet
1-- red
1-- yellow

Is there any way to adapt this jTree print code but only with the selected nodes?

The following code returns an Objects with the paths:

TreePath[] checkedPaths = checkTreeManager.getSelectionModel().getSelectionPaths();
         for (TreePath s : checkedPaths) {
         System.out.println(s);
            }
         }

In the case of Jtree who posted the output of the previous code is:

[JTree, colors, blue]
[JTree, colors, violet]
  • Txii recursiveness is not easy ;) do you know if a Node is selected or not? Is this code you know? if yes publish the code as you do... (is that I do not do a damn ever used those jtree)

  • 1

    I’ll put the code in the question !

  • I have thought of some ways to solve this, but I think they are all calling by the name "GAMBIARRAS";) for example one of them, put this last code in a function in which enters as parameter that number of the recursive function, verificaIsSelecionado(TreeNode nome) and then check if those Ode exists within the checkedPaths If yes you print if no, do not print... As said is a half solution Gambiarra but do not know any better

  • The question is how I later get Node from checkedPaths if what it returns is an object for example:[Jtree, Colors, blue]. If I do a checkISelected(blue); how do I go through Path [Jtree, Colors, blue] ?

  • Because I hadn’t thought about it, but you have to have some method that gives you back the Ode, because it knows the way to it so it can also return you. (I say)

  • [Jtree, Colors, blue] that’s an Object because I tried that scene you taught me: for (Treepath s : checkedPaths) { if(s instanceof Object) System.out.println("I’m in"); } And it assumes to be an object, there’s a way for me to walk through that object ?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.