简体   繁体   中英

JTree not displaying added nodes

I'm trying to create my first JTree , but when I try to add my nodes to the tree, the changes aren't taking effect and all that's displayed is a tree with some default nodes in it (colors, sports, food).

My main panel code in which the tree resides:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Scenario1Panel extends JPanel
{

/**
 * 
 */
private static final long serialVersionUID = 1L;

public Scenario1Tree scenario1Tree;

public Scenario1Panel()
{
    scenario1Tree = new Scenario1Tree();
    add(scenario1Tree);
}
public static void main(String args[]) 
{
    try
    {
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        System.out.println(lookAndFeel);
        UIManager.setLookAndFeel(lookAndFeel);
    }
    catch(Exception e)
    {
        System.out.println("couldn't get that LookAndFeel");
    }

    Scenario1Panel mgr = new Scenario1Panel();

    JFrame frame = new JFrame();

    frame.add(mgr);
    frame.pack();
    frame.setVisible(true);

}

}

My tree code is:

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class Scenario1Tree extends JTree
{
/**
 * 
 */
private static final long serialVersionUID = 1L;

private JTree tree;

public Scenario1Tree()
{
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario1");
    createNodes(top);
    tree = new JTree(top);

    //Create a tree that allows one selection at a time.
    tree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);


}

private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode loginUser1 = null;
    DefaultMutableTreeNode addNewUsers = null;
    DefaultMutableTreeNode addNewUsersBreakout = null;

    loginUser1 = new DefaultMutableTreeNode("Login User 1");
    top.add(logintUser1);

    addNewUsers = new DefaultMutableTreeNode("Add New Users");
    top.add(addNewUsers);

    addNewUsersBreakout = new DefaultMutableTreeNode("Add User2");
    addNewUsers.add(addNewUsersBreakout );
    addNewUsersBreakout = new DefaultMutableTreeNode("Add User3");
    addNewUsers.add(addNewUsersBreakout );
    addNewUsersBreakout = new DefaultMutableTreeNode("Add User4");
    addNewUsers.add(addNewUsersBreakout );
    addNewUsersBreakout = new DefaultMutableTreeNode("Add User5");
    addNewUsers.add(addNewUsersBreakout );

}

public static void main(String args[]) 
{
    try
    {
        String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
        System.out.println(lookAndFeel);
        UIManager.setLookAndFeel(lookAndFeel);
    }
    catch(Exception e)
    {
        System.out.println("couldn't get that LookAndFeel");
    }

    Scenario1Tree tree = new Scenario1Tree();

    JFrame frame = new JFrame();

    frame.add(tree);
    frame.pack();
    frame.setVisible(true);

}

}

Could someone please explain what I'm doing wrong?

You're creating one JTree too many. You're adding all your nodes to a JTree variable and yet using a JTree extended class for the displayed tree, the latter of which has had no nodes added to it.

I suggest that you do not have your class extend JTree, that you instead give it a method, say getTree() that returns the JTree when and where needed.

ie,

// get rid of extends JTree
public class Scenario1Tree // extends JTree
{
private static final long serialVersionUID = 1L;

private JTree tree;

public Scenario1Tree()
{
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Scenario1");
    createNodes(top);
    tree = new JTree(top);

    tree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);

    // JScrollPane treeView = new JScrollPane(tree);

}

public JTree getTree() {
  return tree;
}

In the Scenario1Tree instead of JTree instance creating call

setModel(new DefaultTreeModel(top));

In fact you don't need the JTree instance at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM