How can I get all paths of a Jtree including the nodes that
have parents other than root?
I did some research and the logic to have access to all nodes is quite simple.
First, get the root of his JTree
using the method getRoot
:
TreeModel theRoot = (TreeModel) tree.getModel().getRoot();
Then just count how many child elements this root has, for this there is the method getChildCount
:
int childCount = tree.getModel().getChildCount(theRoot);
Having the amount of child elements and the root, just create a loop of 0 up to the amount of child elements, with this you can access each index through the method getChild
.
for(int i = 0; i < childCount; i++)
System.out.println( tree.getModel().getChild(theRoot, i) ); // faz algo...
To access the nodes within the child elements, just do the same process:
- Count how many elements there are internally
- Start a loop from 0 to the number of elements.
How can I have access to the example path or index ?
// Acessando o filho 'Exemplo1 -> 1'
String primeiro = tree.getModel().getChild(Exemplo1, 0).toString(); // O primeiro indice inicia-se em '0'
//Acessando o filho 'Exemplo2 -> 2'
String segundo = tree.getModel().getChild(Exemplo2, 0).toString();
I put together a simple test code containing the same JTree
created by you in the question, follows the image:
The example is simple: It will access the two child elements (Example1 > 1 and Example 2 > 2) and store the name in a String
. The exit is:
1
2
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class MyWindow extends JFrame {
public MyWindow() {
super("StackOverflow");
init();
}
private void init(){
// Propriedades do JFrame
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
DefaultMutableTreeNode theRoot = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode theFirstExample = new DefaultMutableTreeNode("Exemplo1");
DefaultMutableTreeNode firstExampleChild = new DefaultMutableTreeNode("1");
theFirstExample.add(firstExampleChild);
DefaultMutableTreeNode theSecondExample = new DefaultMutableTreeNode("Exemplo2");
DefaultMutableTreeNode secondExampleChild = new DefaultMutableTreeNode("2");
theSecondExample.add(secondExampleChild);
theRoot.add(theFirstExample);
theRoot.add(theSecondExample);
JTree theTree = new JTree(theRoot);
getContentPane().add(theTree);
// Acessando o filho 'Exemplo1 -> 1'
String exampleOne = theTree.getModel().getChild(theFirstExample, 0).toString();
//Acessando o filho 'Exemplo2 -> 2'
String exampleTwo = theTree.getModel().getChild(theSecondExample, 0).toString();
System.out.println(exampleOne);
System.out.println(exampleTwo);
}
public static void main(String[] args) {
new MyWindow().setVisible(true);
}
}
@Renan Why the "reward" in a question you yourself answered? There are still doubts? I hope you get your answer :)
– Guilherme Nascimento
@But you threw the reward at 17:15 (UTC) and Matthew answered at 16:24 (UTC). I am confused.
– Guilherme Nascimento
@Although something like "1 new answer" or "2 new answers" always appears without refresh, it seems plausible, ok, I’m glad you found the solution, even more. + 1 in his reply
– Guilherme Nascimento