Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:01:28+00:00 2026-06-16T03:01:28+00:00

I have two strings node1/node2/node3/node4 and node1/node2/node5/node6…. how can I build a ONE JTree

  • 0

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….

import java.awt.*;  
import java.util.ArrayList;

import javax.swing.*;  
import javax.swing.tree.*;  

public class PathTest  
{  
    public PathTest()  
    {  

    DefaultMutableTreeNode   node = buildNodeFromString();   


        DefaultTreeModel model = new DefaultTreeModel(node);  
        JTree tree = new JTree(model);  
        JFrame f = new JFrame();  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        f.add(tree);  
        f.setSize(300,300);  
        f.setLocation(200,200);  
        f.setVisible(true);  
    }  

private DefaultMutableTreeNode buildNodeFromString() {



String qqq= "node1/node2/node3/node4";
DefaultMutableTreeNode node, lastNode = null, root = null;

    String[] s = qqq.split("/");
    for (String str : s) {
    node = new DefaultMutableTreeNode(str);     
    if (root == null)
        root = node;
    if (lastNode != null)
        lastNode.add(node);
    lastNode = node;
    }

return root;
}

    public static void main(String[] args)  
    {  
        new PathTest();  
    }  
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:01:29+00:00Added an answer on June 16, 2026 at 3:01 am

    Try this out, this code creates one root for the tree, and then starts to add the string under it, if you don’t want that root to show, simply call tree.setRootVisible(false);

    import java.util.Enumeration;
    
    import javax.swing.JFrame;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    
    public class PathTest {
        public PathTest() {
            // Create the root node, I'm assuming that the delimited strings will have
            // different string value at index 0
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT");
    
            // Create the tree model and add the root node to it
            DefaultTreeModel model = new DefaultTreeModel(root);
    
            // Create the tree with the new model
            JTree tree = new JTree(model);
    
            // Build the tree from the various string samples
            buildTreeFromString(model, "Node 1/Node 2/Node 3/Node 4");
            buildTreeFromString(model, "Node 1/Node 2/Node 3/Node 5");
            buildTreeFromString(model, "Node 1/Node 2/Node 3/Node 6");
            buildTreeFromString(model, "Node 1/Node 2/Node 4/Node 5");
            buildTreeFromString(model, "Node 1/Node 1/Node 3/Node 5");
    
            // UI
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(tree);
            f.setSize(300, 300);
            f.setLocation(200, 200);
            f.setVisible(true);
        }
    
        /**
         * Builds a tree from a given forward slash delimited string.
         * 
         * @param model The tree model
         * @param str The string to build the tree from
         */
        private void buildTreeFromString(final DefaultTreeModel model, final String str) {
            // Fetch the root node
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    
            // Split the string around the delimiter
            String [] strings = str.split("/");
    
            // Create a node object to use for traversing down the tree as it 
            // is being created
            DefaultMutableTreeNode node = root;
    
            // Iterate of the string array
            for (String s: strings) {
                // Look for the index of a node at the current level that
                // has a value equal to the current string
                int index = childIndex(node, s);
    
                // Index less than 0, this is a new node not currently present on the tree
                if (index < 0) {
                    // Add the new node
                    DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
                    node.insert(newChild, node.getChildCount());
                    node = newChild;
                }
                // Else, existing node, skip to the next string
                else {
                    node = (DefaultMutableTreeNode) node.getChildAt(index);
                }
            }
        }
    
        /**
         * Returns the index of a child of a given node, provided its string value.
         * 
         * @param node The node to search its children
         * @param childValue The value of the child to compare with
         * @return The index
         */
        private int childIndex(final DefaultMutableTreeNode node, final String childValue) {
            Enumeration<DefaultMutableTreeNode> children = node.children();
            DefaultMutableTreeNode child = null;
            int index = -1;
    
            while (children.hasMoreElements() && index < 0) {
                child = children.nextElement();
    
                if (child.getUserObject() != null && childValue.equals(child.getUserObject())) {
                    index = node.getIndex(child);
                }
            }
    
            return index;
        }
    
        public static void main(String[] args) {
            new PathTest();
        }
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two strings, one a key and one a value, which I would
Let's say I have two strings: one is XML data and the other is
We have two sets, A and B. Each one of these sets include strings.
I have two strings str1 and str2 . Check both of them and see
I have two strings in a java program, which I want to mix in
I have two strings String s1=426F62; String s2=457665; The strings are in hex representation.
i have two strings in a php i want to make following checks in
If I have two strings that are identical in value, is it guaranteed that
Let's say I have two strings like this: XABY XBAY A simple regex that
Lets say that I have two strings: The System is in halt state. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.