JTree不显示添加的节点

时间:2013-01-09 17:53:03

标签: java swing jtree treenode

我正在尝试创建我的第一个JTree,但是当我尝试将我的节点添加到树中时,更改没有生效,所有显示的都是一个带有一些默认节点的树(颜色,运动,食品)。

树所在的主面板代码:

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);

}

}

我的树代码是:

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);

}

}

有人可以解释一下我做错了什么吗?

2 个答案:

答案 0 :(得分:6)

你创建的一个JTree太多了。您将所有节点添加到JTree变量,然后对显示的树使用JTree扩展类,后者没有添加任何节点。

我建议你没有让你的类扩展JTree,而是给它一个方法,比如getTree(),它在需要的时间和地点返回JTree。

即,

// 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;
}

答案 1 :(得分:5)

Scenario1Tree代替JTree实例创建调用

setModel(new DefaultTreeModel(top));

事实上,您根本不需要JTree个实例。