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.
Solution 1:
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");
Solution 2:
Make a recursive function that takes in the root node, adds nodes for each file/dir underneath it, and then calls itself again on each of those nodes.
Edit: no need to inherit from DefaultMutableTreeNode if each node already contains the relative path.