Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swdev:java:component:jtree [2017/10/02 08:50] smayr created |
swdev:java:component:jtree [2017/10/08 11:25] (current) smayr [File List in JTree] |
||
---|---|---|---|
Line 42: | Line 42: | ||
}); | }); | ||
- | } | ||
- | |||
- | @Override | ||
- | public Dimension getPreferredSize() { | ||
- | return new Dimension(200, | ||
} | } | ||
Line 60: | Line 55: | ||
} | } | ||
+ | } | ||
</ | </ | ||
Reference: [[https:// | Reference: [[https:// | ||
+ | |||
+ | == File List in Tree == | ||
+ | <code java> | ||
+ | package com.ggl.testing; | ||
+ | |||
+ | import java.io.File; | ||
+ | |||
+ | import javax.swing.JFrame; | ||
+ | import javax.swing.JScrollPane; | ||
+ | import javax.swing.JTree; | ||
+ | import javax.swing.SwingUtilities; | ||
+ | import javax.swing.tree.DefaultMutableTreeNode; | ||
+ | import javax.swing.tree.DefaultTreeModel; | ||
+ | |||
+ | public class FileBrowser implements Runnable { | ||
+ | |||
+ | private DefaultMutableTreeNode root; | ||
+ | |||
+ | private DefaultTreeModel treeModel; | ||
+ | |||
+ | private JTree tree; | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | JFrame frame = new JFrame(" | ||
+ | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | |||
+ | File fileRoot = new File(" | ||
+ | root = new DefaultMutableTreeNode(new FileNode(fileRoot)); | ||
+ | treeModel = new DefaultTreeModel(root); | ||
+ | |||
+ | tree = new JTree(treeModel); | ||
+ | tree.setShowsRootHandles(true); | ||
+ | JScrollPane scrollPane = new JScrollPane(tree); | ||
+ | |||
+ | frame.add(scrollPane); | ||
+ | frame.setLocationByPlatform(true); | ||
+ | frame.setSize(640, | ||
+ | frame.setVisible(true); | ||
+ | |||
+ | CreateChildNodes ccn = | ||
+ | new CreateChildNodes(fileRoot, | ||
+ | new Thread(ccn).start(); | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | SwingUtilities.invokeLater(new FileBrowser()); | ||
+ | } | ||
+ | |||
+ | public class CreateChildNodes implements Runnable { | ||
+ | |||
+ | private DefaultMutableTreeNode root; | ||
+ | |||
+ | private File fileRoot; | ||
+ | |||
+ | public CreateChildNodes(File fileRoot, | ||
+ | DefaultMutableTreeNode root) { | ||
+ | this.fileRoot = fileRoot; | ||
+ | this.root = root; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void run() { | ||
+ | createChildren(fileRoot, | ||
+ | } | ||
+ | |||
+ | private void createChildren(File fileRoot, | ||
+ | DefaultMutableTreeNode node) { | ||
+ | File[] files = fileRoot.listFiles(); | ||
+ | if (files == null) return; | ||
+ | |||
+ | for (File file : files) { | ||
+ | DefaultMutableTreeNode childNode = | ||
+ | new DefaultMutableTreeNode(new FileNode(file)); | ||
+ | node.add(childNode); | ||
+ | if (file.isDirectory()) { | ||
+ | createChildren(file, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | public class FileNode { | ||
+ | |||
+ | private File file; | ||
+ | |||
+ | public FileNode(File file) { | ||
+ | this.file = file; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public String toString() { | ||
+ | String name = file.getName(); | ||
+ | if (name.equals("" | ||
+ | return file.getAbsolutePath(); | ||
+ | } else { | ||
+ | return name; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | References: [[https:// | ||
+ | |||
+ | == File List in JTree == | ||
+ | <code java> | ||
+ | import java.io.File; | ||
+ | import java.io.IOException; | ||
+ | import java.nio.file.Files; | ||
+ | import java.nio.file.Path; | ||
+ | import java.nio.file.Paths; | ||
+ | import javax.swing.DefaultListModel; | ||
+ | import javax.swing.JTree; | ||
+ | import javax.swing.tree.DefaultMutableTreeNode; | ||
+ | import javax.swing.tree.DefaultTreeModel; | ||
+ | //... | ||
+ | |||
+ | public class frmMain extends javax.swing.JFrame | ||
+ | { | ||
+ | public frmMain() | ||
+ | { | ||
+ | initComponents(); | ||
+ | | ||
+ | LaunchWebView(); | ||
+ | OpenXmlDataFile(); | ||
+ | | ||
+ | try { | ||
+ | | ||
+ | | ||
+ | } catch (Exception exc) { | ||
+ | // Do something | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <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 class FileUtils | ||
+ | { | ||
+ | public static void scanAndLoadDirectory(String location, JTree tree) throws InterruptedException | ||
+ | { | ||
+ | // Creates a file with the location filename | ||
+ | //String location =" | ||
+ | File currentDir = new File(location); | ||
+ | |||
+ | // Get root of tree model | ||
+ | DefaultTreeModel modelTree = (DefaultTreeModel)tree.getModel(); | ||
+ | DefaultMutableTreeNode root = (DefaultMutableTreeNode) modelTree.getRoot(); | ||
+ | | ||
+ | displayDirectoryContents(currentDir, | ||
+ | } | ||
+ | | ||
+ | public static void displayDirectoryContents(File dir, DefaultTreeModel model, DefaultMutableTreeNode curRoot) throws InterruptedException | ||
+ | { | ||
+ | File[] files = dir.listFiles(); | ||
+ | |||
+ | for (File file : files) { | ||
+ | if (file == null) { | ||
+ | System.out.println(" | ||
+ | continue; | ||
+ | } | ||
+ | if (file.isDirectory()) { | ||
+ | // | ||
+ | // File is a Directory | ||
+ | // | ||
+ | if (file.listFiles() == null) { | ||
+ | // Skip null files | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | // Adds a new Directory node to tree root | ||
+ | DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName()); | ||
+ | curRoot.add(newdir); | ||
+ | |||
+ | // Refresh the model to show changes | ||
+ | model.reload(); | ||
+ | |||
+ | // Recursively calls the function again to explore the contents folder | ||
+ | displayDirectoryContents(file, | ||
+ | | ||
+ | } else { | ||
+ | // | ||
+ | // File is not a directory | ||
+ | // | ||
+ | // Inserts a node newfile under selected node which is the root | ||
+ | DefaultMutableTreeNode selectednode = curRoot; | ||
+ | DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName()); | ||
+ | model.insertNodeInto(newfile, | ||
+ | |||
+ | // Refresh the model to show the changes | ||
+ | model.reload(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |