Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
swdev:java:component:jtree [2017/10/08 10:46] smayr [File List in JTree] |
swdev:java:component:jtree [2017/10/08 11:25] (current) smayr [File List in JTree] |
||
---|---|---|---|
Line 190: | Line 190: | ||
try { | try { | ||
| | ||
- | scanDirectory(" | + | FileUtils.scanAndLoadDirectory(" |
} catch (Exception exc) { | } catch (Exception exc) { | ||
// Do something | // Do something | ||
Line 196: | Line 196: | ||
} | } | ||
- | | + | } |
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package com.acme.myapp; | ||
+ | |||
+ | import java.io.File; | ||
+ | import javax.swing.JTree; | ||
+ | import javax.swing.tree.DefaultMutableTreeNode; | ||
+ | import javax.swing.tree.DefaultTreeModel; | ||
+ | |||
+ | public | ||
+ | { | ||
+ | public static | ||
{ | { | ||
// Creates a file with the location filename | // Creates a file with the location filename | ||
Line 202: | Line 215: | ||
File currentDir = new File(location); | File currentDir = new File(location); | ||
- | // Result is the variable name for jtree | + | // Get root of tree model |
- | DefaultTreeModel | + | DefaultTreeModel |
- | // Gets the root of the current model used only once at the starting | + | DefaultMutableTreeNode root = (DefaultMutableTreeNode) |
- | DefaultMutableTreeNode root = (DefaultMutableTreeNode) | + | |
| | ||
- | displayDirectoryContents(currentDir, | + | displayDirectoryContents(currentDir, |
} | } | ||
| | ||
- | | + | |
{ | { | ||
File[] files = dir.listFiles(); | File[] files = dir.listFiles(); | ||
for (File file : files) { | for (File file : files) { | ||
- | if(file == null) { | + | if (file == null) { |
System.out.println(" | System.out.println(" | ||
continue; | continue; | ||
} | } | ||
if (file.isDirectory()) { | if (file.isDirectory()) { | ||
- | // Folder | + | //--------------------------- |
+ | // File is a Directory | ||
+ | // | ||
if (file.listFiles() == null) { | if (file.listFiles() == null) { | ||
// Skip null files | // Skip null files | ||
Line 226: | Line 240: | ||
} | } | ||
- | // Adds a node to the root of the JTree | + | // Adds a new Directory |
- | DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); | + | |
DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName()); | DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName()); | ||
- | | + | |
- | // Refresh the model to show the changes | + | // Refresh the model to show changes |
model.reload(); | model.reload(); | ||
// Recursively calls the function again to explore the contents folder | // Recursively calls the function again to explore the contents folder | ||
- | displayDirectoryContents(file, | + | displayDirectoryContents(file, |
| | ||
- | } else { // File is not a directory | + | } else { |
- | + | // | |
- | // Gets the current model of the tree | + | |
- | DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); | + | //--------------------------- |
- | + | // Inserts a node newfile under selected node which is the root | |
- | // Selected | + | DefaultMutableTreeNode selectednode = curRoot; |
- | DefaultMutableTreeNode selectednode = treeRoot; | + | |
DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName()); | DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName()); | ||
- | |||
- | // Inserts a node newfile under selected node which is the root | ||
model.insertNodeInto(newfile, | model.insertNodeInto(newfile, | ||
Line 254: | Line 264: | ||
} | } | ||
} | } | ||
- | </ | + | } |
+ | </ |