JTree在启动应用程序时添加节点 [英] JTree add nodes on startup of application

查看:112
本文介绍了JTree在启动应用程序时添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用文件浏览器制作文本编辑器,所以当我启动我的应用程序时,我希望我的程序在JTree上添加节点,这样就可以在My Documents文件夹中显示所有文件和文件夹,并让我能够访问那些文件和文件夹(特别是文件夹)。我试图弄清楚Andrew Thompson是如何通过此示例来做到这一点的。
但我失败了。我设法使用这个例子
。但就是这样,当我点击其中一个代表文件夹的节点时,我无法弄清楚如何为其他文件和文件夹生成节点。

I want to make text editor with file browser so when I start my application I want to my program add nodes on JTree so it shows me all files and folders for example in My Documents folder, and to give me ability to access to those files and folders (especially to folders). I tried to figure out how Andrew Thompson did that from this example but I failed. I managed to create nodes for all files and folders from My Documents using this example . But thats all, I can't figure out how to generate nodes for other files and folders when clicking on one of nodes which represent folder.

这就是我的意思到目前为止:

This is what I've done till now:

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;


public class MyTextEditor extends JFrame{

    JTree tree;
    JTabbedPane tabbedPane = new JTabbedPane();
    File myDocumentsFolder = new File("C:/Documents and Settings/User/My Documents");
    File[] listOfFiles = myDocumentsFolder.listFiles();
    String dirTitle = myDocumentsFolder.getName();
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(dirTitle);
    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);

    public MyTextEditor() {

        tree = new JTree(treeModel);
        tree.setEditable(false);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(tree),tabbedPane);
        add(splitPane);

        tree.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                for (int i = 0; i < listOfFiles.length; i++) {
                    String nameOfFile = listOfFiles[i].getName();
                    rootNode.add(new DefaultMutableTreeNode(nameOfFile));
                }
            }
        });

    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                MyTextEditor mte = new MyTextEditor();
                mte.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mte.setPreferredSize(new Dimension(800,600));
                mte.pack();
                mte.setLocationByPlatform(true);
                mte.setVisible(true);
            }
        });
    }

}

有人可以告诉我如何生成特定文件夹的所有文件和文件夹的节点。在此先感谢。

Can someone tell me how to generate nodes for all files and folders for specific folder. Thanks in advance.

推荐答案

我用这个 FileTreeModel TreeModel 大纲 ,以及 user.dir 用于起始目录。

I use this FileTreeModel for the TreeModel, Outline for the view, and user.dir for the starting directory.

TreeModel treeModel = new FileTreeModel(
    new File(System.getProperty("user.dir")));
OutlineModel outlineModel = DefaultOutlineModel.createOutlineModel(
    treeModel, new FileRowModel(), true, "User Directory");

这篇关于JTree在启动应用程序时添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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