如何动态构建jtree [英] How to build a jtree dynamically

查看:70
本文介绍了如何动态构建jtree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的SSCCE如下.我正在动态填充JTree,但是没有任何反应.

The SSCCE of the problem is as follows. I am dynamically populating JTree, but nothing is happening.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class DynamicTreeDemo extends JFrame
                        implements ActionListener {
private int newNodeSuffix = 1;
private static String ADD_COMMAND = "add";
private static String REMOVE_COMMAND = "remove";
private static String CLEAR_COMMAND = "clear";

private JTree jtrMainMenu;
DefaultMutableTreeNode   rootNode = null;
DefaultTreeModel treeModel;
public DynamicTreeDemo() {
    super(new BorderLayout());

    //Create the components.
    jtrMainMenu = new JTree();
    populateTree();

    JButton addButton = new JButton("Add");
    addButton.setActionCommand(ADD_COMMAND);
    addButton.addActionListener(this);

    JButton removeButton = new JButton("Remove");
    removeButton.setActionCommand(REMOVE_COMMAND);
    removeButton.addActionListener(this);

    JButton clearButton = new JButton("Clear");
    clearButton.setActionCommand(CLEAR_COMMAND);
    clearButton.addActionListener(this);

    //Lay everything out.
    jtrMainMenu.setPreferredSize(new Dimension(300, 150));
    add(jtrMainMenu, BorderLayout.CENTER);

    JPanel panel = new JPanel(new GridLayout(0,3));
    panel.add(addButton);
    panel.add(removeButton); 
    panel.add(clearButton);
add(panel, BorderLayout.SOUTH);
}


    public void populateTree() {
  Vector<Vector<String>> data =GetDataFromDB();           
 java.util.List<String> list =GetListFromDB();
        int i=0;;
 int size=list.size();
 treeModel=(DefaultTreeModel)jtrMainMenu.getModel();  

 ((DefaultMutableTreeNode)  (treeModel.getRoot())).removeAllChildren();
 treeModel.reload();
 rootNode = new DefaultMutableTreeNode("JewelleryERPApplication");
 treeModel=new DefaultTreeModel(rootNode);


 DefaultMutableTreeNode p1=null, p2=null,p3=null,p4=null,p5=null;


     while(i<size){

        if(list.get(i).length()==2){
          p1 = addObject(null, data.get(i).elementAt(1),true);  

        }else 
        if(list.get(i).length()==4){
           p2 = addObject(p1, data.get(i).elementAt(1),true);   

     }else if
       (list.get(i).length()==6){
             p3 = addObject(p2, data.get(i).elementAt(1),true);   

     }
     else if
       (list.get(i).length()==8){
             p4 = addObject(p3, data.get(i).elementAt(1),true);    

     }  
     else if
       (list.get(i).length()==10){
             p5 = addObject(p4, data.get(i).elementAt(1),true);    

     }      
         i++;
     }

    jtrMainMenu.setModel(treeModel);
   ((DefaultMutableTreeNode)  (treeModel.getRoot())).getChildCount();
    jtrMainMenu.revalidate();
    jtrMainMenu.repaint();
}


public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                        Object child, 
                                        boolean shouldBeVisible) {


          DefaultMutableTreeNode childNode = 
            new DefaultMutableTreeNode(child);



    if (parent == null) {
        parent = rootNode;
    }

//It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
    treeModel.insertNodeInto(childNode, parent, 
                             parent.getChildCount());

     jtrMainMenu.setModel(treeModel);

    //Make sure the user can see the lovely new node.
    if (shouldBeVisible) {
        jtrMainMenu.scrollPathToVisible(new TreePath(childNode.getPath()));
    }

    return childNode;
} 

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("DynamicTreeDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    DynamicTreeDemo newContentPane = new DynamicTreeDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

推荐答案

您的代码有很多问题,但是...(我认为)您遇到的主要问题是,当您重新加载代码时模型,默认情况下根节点是折叠的.

There are a number of problems with you code, but...the major problem (I think) you're having is the fact that when you reload the model, the root node is collapsed by default.

如果根节点是隐藏的和/或它的句柄是隐藏的,则它看起来就像没有加载任何东西.

If the root node is hidden and/or it's handle is hidden, it will appear as if nothing has been loaded.

取消隐藏这些元素进行测试.

Unhide these elements for testing.

一旦重新加载模型,您也可以扩展根节点...

You can also expand the root node once the model has been reloaded...

!!警告!! 请勿在您的根目录上运行该命令!它将扫描所有子目录,这可能需要更多时间,因此您实际上愿意等待:P

!! Warning !! Don't run this on your root directory !! It will scan all child directories and that could take more time then you're actually willing to wait for :P

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class FileTree {

    public static void main(String[] args) {
        new FileTree();
    }

    public FileTree() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private DefaultTreeModel model;
        private JTree tree;

        public TestPane() {
            setLayout(new BorderLayout());

            tree = new JTree();
            File rootFile = new File(".");
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootFile);
            model = new DefaultTreeModel(root);

            tree.setModel(model);
            tree.setRootVisible(true);
            tree.setShowsRootHandles(true);

            add(new JScrollPane(tree));

            JButton load = new JButton("Load");
            add(load, BorderLayout.SOUTH);

            load.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
                    root.removeAllChildren();
                    model.reload();
                    File rootFile = (File) root.getUserObject();

                    addFiles(rootFile, model, root);

                    tree.expandPath(new TreePath(root));

                }
            });

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void addFiles(File rootFile, DefaultTreeModel model, DefaultMutableTreeNode root) {

            for (File file : rootFile.listFiles()) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(file);
                model.insertNodeInto(child, root, root.getChildCount());
                if (file.isDirectory()) {
                    addFiles(file, model, child);
                }
            }

        }
    }
}

代码审查

您应该避免直接从JFrame扩展.从您的外观来看,您虽然不是,但还是将其入侵了,但是还是不建议的……

You should avoid extending directly from JFrame. From the looks of you example, you weren't, but hacked it in, but it's inadvisable anyway...

这个...

jtrMainMenu.setPreferredSize(new Dimension(300, 150));
add(jtrMainMenu, BorderLayout.CENTER);

这是不可取的. JTree应该添加到JScrollPane.

This is inadvisable. JTree should be added to JScrollPane.

这个...

private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("DynamicTreeDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    BadTree newContentPane = new BadTree();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

也不起作用(根据您的示例),因为从Window扩展的任何内容都无法添加到任何扩展Window

Isn't going to work either (based on your example), as anything that extends from Window can not be added to any other container that extends Window

除非您的基本组件是从JComponent扩展的,否则其他组件必须是不透明的(JLabel是一个明显的例外),因此将newContentPane设置为不透明可能是一个静音点.

Unless your base component is extending from JComponent, must other components are opaque (JLabel is an obvious exception), so setting the newContentPane to opaque may be a mute point.

这篇关于如何动态构建jtree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆