0

I'm trying to implement a splitpane with scrollpanes on both sides. The left side should show a JTree that I'm attempting to implement but it's not working and I am unable to see the tree.

I'm not sure what I'm doing wrong. My code is something along the lines of:

public class SplitPane extends JFrame {
    DefaultTreeModel treeModel;
    JEditorPane editorPane = new JEditorPane();
    DefaultMutableTreeNode Root;
    JTree tree;
    JScrollPane leftscrollPane;
    JScrollPane rightscrollPane;

    public SplitPane() {
        setSize(600,400);
        tree = new JTree(Root);
        Root = new DefaultMutableTreeNode("");
        setTree();                   // I connect all the nodes here
        treeModel = new DefaultTreeModel(Root);
        tree = new JTree(Root);
        tree.setRootVisible(false);

        leftscrollPane = new JScrollPane(tree);
        rightscrollPane = new JScrollPane(editorPane);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(leftscrollPane);
        splitPane.setRightComponent(rightscrollPane);

        splitPane.setDividerLocation(160);

        setVisible(true);

        splitPane.setPreferredSize(new Dimension(600,400));
        getContentPane().add(splitPane);
    }
}

Then to initialize, I just do SplitPane newpane = new SplitPane();

I think I have all the nodes added properly because when I do

Enumeration e = Root.preorderEnumeration();
while(e.hasMoreElements()) {
    System.out.println(e.nextElement());
}

I see all the nodes in order.

What am I doing wrong? I would really appreciate some help and feedback!

jaewhyun
  • 71
  • 2
  • 11

1 Answers1

0

Your second

tree = new JTree(Root);

has to be initialized with the model:

tree = new JTree(treeModel);

You can remove the first initialization of tree, because it is overwritten by the second one.

Here is a running example:

public class SplitPane extends JFrame {

  private MutableTreeNode createTree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    root.add(new DefaultMutableTreeNode("child 1"));
    root.add(new DefaultMutableTreeNode("child 2"));

    return root;
  }

  public SplitPane() {

    setSize(600, 400);

    // create model and add nodes
    TreeModel model = new DefaultTreeModel(createTree());

    // initialize tree, set the model
    JTree tree = new JTree(model);
    tree.setRootVisible(false);

    JScrollPane leftScrollPane = new JScrollPane(tree);
    JScrollPane rightScrollPane = new JScrollPane(new JLabel("Text ..."));

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(leftScrollPane);
    splitPane.setRightComponent(rightScrollPane);
    splitPane.setDividerLocation(200);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().add(splitPane);
    this.setVisible(true);
  }
}

You can run this example by simply instanciating it somewhere like this:

        SplitPane splitPane = new SplitPane();
ChristophS
  • 598
  • 4
  • 23