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

  • Home
  • SEARCH
  • 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 8096563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:29:27+00:00 2026-06-05T21:29:27+00:00

Can anyone provide a working sample code to convert following Oracle DB table with

  • 0

Can anyone provide a working sample code to convert following Oracle DB table with parent-child relation to a JTree in Java code?

Table T (columns: Id, Parent_Id, Description).

I have searched many similar questions but all seems vague and incomplete for someone who is new to JTree. I dont need any code to connect to DB but only to go over the results and populating the JTree.

Thanks

  • 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-05T21:29:29+00:00Added an answer on June 5, 2026 at 9:29 pm

    When I started with JTree I implemented this class that seems pretty similar with your problem.

    Check methods: getElementTreeFromPlainList that is more or less the method that you would use to fetch data from persistent layer and method createTreeNodesForElement that creates the nodes for the tree.

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.swing.JFrame;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    
    public class MyJFrame extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        private static List<String[]> data = new ArrayList<String[]>();
    
        static {
            data.add(new String[] { "Id1", "Start 1", null });
            data.add(new String[] { "Id2", "Node 1.1", "Id3" });
            data.add(new String[] { "Id3", "Node 1", "Id1" });
            data.add(new String[] { "Id4", "Node 1.2", "Id3" });
            data.add(new String[] { "Id5", "Node 2", "Id1" });
            data.add(new String[] { "Id6", "Start 2", null });
        }
    
        public static void main(final String[] args) {
            new MyJFrame();
        }
    
        MyJFrame() {
            JTree tree = new JTree();
    
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Found Nodes");
    
            createTreeNodesForElement(root, getElementTreeFromPlainList());
    
            tree.setModel(new DefaultTreeModel(root));
    
            add(tree);
    
            setVisible(true);
            setBounds(0, 0, 200, 200);
        }
    
        Collection<Element> getElementTreeFromPlainList() {
            // builds a map of elements object returned from store
            Map<String, Element> values = new HashMap<String, Element>();
            for (String[] s : data) {
                values.put(s[0], new Element(s[2], s[1]));
            }
    
            // creates a result list
            Collection<Element> result = new ArrayList<MyJFrame.Element>();
    
            // for each element in the result list that has a parent, put it into it
            // otherwise it is added to the result list
            for (Element e : values.values()) {
                if (e.parent != null) {
                    values.get(e.parent).getChildren().add(e);
                } else {
                    result.add(e);
                }
            }
    
            return result;
        }
    
        void createTreeNodesForElement(final DefaultMutableTreeNode dmtn, final Collection<Element> elements) {
            // for each element a tree node is created
            for (Element child : elements) {
                DefaultMutableTreeNode created = new DefaultMutableTreeNode(child.getName());
                dmtn.add(created);
                createTreeNodesForElement(created, child.getChildren());
            }
        }
    
        public static class Element {
            private final String parent;
            private final String name;
            private final Collection<Element> children = new ArrayList<MyJFrame.Element>();
    
            public Element(final String parent, final String name) {
                super();
                this.parent = parent;
                this.name = name;
            }
    
            public String getName() {
                return name;
            }
    
            public Collection<Element> getChildren() {
                return children;
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please provide me with a sample code for this? I have a
Can anyone provide me with a code snippet of a simple java annotation processor
Can anyone provide a clear example of how to remove a supercolumn in Java
Can anyone provide an explanation for the following phenomenon? As of the iPhone Device
Can anyone provide some links to good information on setting up Silverlight 2.0 to
Can anyone provide me with a hello world example for a major mode in
Can anyone provide me an example of how to use WM_CLOSE to close a
Can anyone provide details on what the three of these mean? System::ContainerStartTime System::CreationDate System::StartTime
Can anyone provide some information on how to run an integration services package, on
Can anyone provide a function to sanitize input for a UniData query? Or provide

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.