Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

java - JTree add nodes on startup of application

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...