0

I have a Array list with two columns one column has child and one has parent e.g

label                     Parent

Test
something 1               Test
Something 2               Test
Something 3               Something 1
Something 4               Something 1
Something 5               Something 1
Something 6               Something 5

Below is the code I am using to create a tree:

DefaultMutableTreeNode root = new DefaultMutableTreeNode(nodeList.get(0).label);
findChild(root, nodeList);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
final JTree tree = new JTree(root);

static void findChild(DefaultMutableTreeNode parent, List<Node> nodeList2) {

    for (int i = 0; i < nodeList2.size(); i++) {
        if (nodeList2.get(i).parent != null && nodeList2.get(i).parent.equals(parent.toString())) {
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeList2.get(i).label);
            Parent.add(child);
            findChild(child, nodeList2); // child of child
        }
    }
}

The issue is I am having just Test displayed as a root node nothing else below it. Any thoughts what I am doing wrong?

0

1 Answer 1

1

This works for me:

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.tree.*;

public class TreeTest {
  public JComponent makeUI() {
    List<Node> nodeList = new ArrayList<>();
    nodeList.add(new Node("Test"));
    //typo? nodeList.add(new Node("something 1", "Test"));
    nodeList.add(new Node("Something 1", "Test"));
    nodeList.add(new Node("Something 2", "Test"));
    nodeList.add(new Node("Something 3", "Something 1"));
    nodeList.add(new Node("Something 4", "Something 1"));
    nodeList.add(new Node("Something 5", "Something 1"));
    nodeList.add(new Node("Something 6", "Something 5"));

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(nodeList.get(0).label);
    findChild(root, nodeList);
    JTree tree = new JTree(root);
    return new JScrollPane(tree);
  }
  private static void findChild(DefaultMutableTreeNode parent, List<Node> list) {
    for (int i = 0; i < list.size(); i++) {
      if (list.get(i).parent != null && list.get(i).parent.equals(parent.toString())) {
        DefaultMutableTreeNode child = new DefaultMutableTreeNode(list.get(i).label);
        parent.add(child);
        findChild(child, list); // child of child
      }
    }
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new TreeTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

class Node {
  public final String label;
  public final String parent;
  public Node(String label) {
    this(label, null);
  }
  public Node(String label, String parent) {
    this.label = label;
    this.parent = parent;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.