Forums Register Login

Building JTree from strings

+Pie Number of slices to send: Send
I have two strings node1/node2/node3/node4" and "node1/node2/node5/node6".... how can I build a ONE JTree in swing from this strings? Here is my Code that builds one string....
+Pie Number of slices to send: Send
Welcome to the ranch.

Your node building method needs to take two parameters, the root node (which will be null on the first call) and the String to break into parts to create the nodes.
Once you have split the string you need to do as you are currently doing but when root is not initially null you need to check each split string value against the user object values initially of the root and subsequently of all the children of the node referenced by a node variable (eg called matchNode) which initially points to the root. If you find a node with that value you know you don't need to add a new node so you set the matchNode variable to the node with the matching user object value and check the next split string value, otherwise do as you are currently doing and create a new node. Once you have failed to find a matching child you can stop looking for any more matches for the rest of the input string and just add nodes.
+Pie Number of slices to send: Send
 

Tony Docherty wrote:Welcome to the ranch.

Your node building method needs to take two parameters, the root node (which will be null on the first call) and the String to break into parts to create the nodes.
Once you have split the string you need to do as you are currently doing but when root is not initially null you need to check each split string value against the user object values initially of the root and subsequently of all the children of the node referenced by a node variable (eg called matchNode) which initially points to the root. If you find a node with that value you know you don't need to add a new node so you set the matchNode variable to the node with the matching user object value and check the next split string value, otherwise do as you are currently doing and create a new node. Once you have failed to find a matching child you can stop looking for any more matches for the rest of the input string and just add nodes.



how it would look in code?
+Pie Number of slices to send: Send
Sorry, I won't write the code for you. But I will help you to write it.

Break down what I have said into small chunks and try to implement them one at a time.
The first part ie adding the method parameters should easy.
The next bit is to create your matchNode variable etc.
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1878 times.
Similar Threads
Table Viewer GUI from a Database
Trying to get manageable arrays to work with
Reference to a String in jni to call a dll function of c++.
JSF 2.0 MyConverter.getAsObject(...) how to get a handle on the backing bean
I can't change card in cardLayout using JTree listener
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:52:58.