How to build a jtree dynamically

后端 未结 1 952
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 03:11

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

import java.awt.BorderLayout;
import ja         


        
相关标签:
1条回答
  • 2021-01-16 03:42

    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...

    enter image description here

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

    Code Review

    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...

    This...

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

    This is inadvisable. JTree should be added to JScrollPane.

    This...

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

    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

    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.

    0 讨论(0)
提交回复
热议问题