How to copy one tree model to another tree model in JTree? - java

I have created two Java trees.
Now I want to assign second JTree to first JTree for my further process. How can I do it?
Clone a java TreeModel or are there any special method to do that?
I used ,
firstTree.setModel(secondTree.getModel());
But not successful.

Can you not use the constructor to initialize JTree with existing model :
firstTree=new Jtree(secondTree.getModel());

Related

Custom Java Swing XML Editor

I need to show an xml as a tree structure in a java swing application. When I click on any node in the swing tree its attribute should list in a window (like a properties editor) and from that I need to update the attributes. Also I need to hide some nodes in the xml in the tree.
I am planning to use JTree to populate and show the xml as tree. But bit confused about what approach need to take to read/update the xml.
Currently I have the below options:
Using JAXB create the corresponding objects and populate the JTree
Use any parser DOM or SAX and populate the JTree using the DefaultMutableTreeNode
Please advice what approach I need to proceed? or is there any other better way for doing this?

How can i access the subnode in Jtree

anyone help me i have Jtree and Subnode i want to click sub node and display one window form in java am using netbeans.
You can achieve this by using a tree selection listener. Look at How to Use Trees tutorial. In particular, Responding to Node Selection section describes how to implement a basic selection listener.

Finding the node where an instance belong

I generated a decision tree based on a set of data, then I converted this data to a xml file, after that I put it into a JTree. This part works fine but now I have to take a new instance(which contains the data received from the user), find it's place in the decision tree and change that node's color. And I can't find a way to do that. I am using weka and the J48 classifier to generate the decision tree. The xml is created in the Luc Sorel style: http://www.lucsorel.com/media/downloads/sample_decision_tree.xml
The first thing I tried was to classify the instance using the algorithm but that gets me only the class where it belongs, and I don't know how to locate it in the JTree.
What should I do? Any ideeas?
Like JTable, JTree uses a flyweight renderer to draw nodes. As the default renderer is a JLabel, you can set the foreground color or make the label opaque and set the background color. This related example changes the icon for emphasis. More examples may be found here.
Addendum: I cannot find … the node that I should color.
When getTreeCellRendererComponent() is called, value is a reference to the node to be rendered, and the tree parameter allows access to the TreeModel as a whole via getModel(). This example shows how to search a tree.

How to create inverted JTree in Java?

Actually I'm interested to create an inverted JTree in Java in such a way that root exists at top and its child nodes at the next level and so on. As we know in case of simple JTree the child nodes appears expanding on the right side of the parent node but I want to implement inverted JTree in my project where child nodes expands downwards which gives an appearance of "TREE" type of data structure.
As this is very essential for my project so can anyone suggest me the code for the above mentioned problem?
Thanks in advance.
Then JTree is not the correct choice. Perhaps, you should look into Java2D and stuff like that.

Java Tree Node selection

I have a tree and a TreePath to one of its nodes. How do I programatically select this node?
Will setSelectionPath of the JTree object alone work?
Yes. From the Java Documentation
If any component of the path is hidden
(under a collapsed node), and
getExpandsSelectedPaths is true it is
exposed (made viewable).

Categories