如何在Eclipse Java WindowBuilder中创建这样的组件?

时间:2015-07-07 13:28:00

标签: java eclipse swing jtree windowbuilder

我想制作一个有很多根的树状组件。我应该使用什么组件? JTree不允许多个父母。

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用普通的JTree实现这一点,并将setRootVisible属性设置为false

 jTree.setRootVisible(false);

实施例

   JFrame frame = new JFrame();

   ///adding mock data
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 1; i < 5; i++) {
        DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Parent" + i);
        for (int j = 1; j < 5; j++) {
            parent.add(new DefaultMutableTreeNode("Child" + j));
        }
        root.add(parent);
    }

    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);

    tree.setRootVisible(false);//To hide root

    frame.add(new JScrollPane(tree));
    frame.setTitle("Tree RootHide Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

示例输出

Tree with root hidden mocks as Multiple root