Hi, I am creating an application where I want to show a file system. I have never worked with them before, so i am not sure on what I need to do to accomplish my goal. So far I found a script through google that lists all the files and folders. What I want to do is modified the script to only show just the folders, then when the user clicks on the folder, I want to populate another object (maybe JList), with only images(jpg,bmp, tiff,png,gif) that are in the folder the user clicked. Any ideas??!?!? Thanks
Code:
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.io.File; import java.util.*; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreeNode; public class FileTreePanel extends JPanel { protected static FileSystemView fsv = FileSystemView.getFileSystemView(); private static class FileTreeCellRenderer extends DefaultTreeCellRenderer { private Map<String, Icon> iconCache = new HashMap<String, Icon>(); private Map<File, String> rootNameCache = new HashMap<File, String>(); public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { FileTreeNode ftn = (FileTreeNode) value; File file = ftn.file; String filename = ""; if (file != null) { if (ftn.isFileSystemRoot) { filename = this.rootNameCache.get(file); if (filename == null) { filename = fsv.getSystemDisplayName(file); this.rootNameCache.put(file, filename); } } else { filename = file.getName(); } } JLabel result = (JLabel) super.getTreeCellRendererComponent(tree, filename, sel, expanded, leaf, row, hasFocus); if (file != null) { Icon icon = this.iconCache.get(filename); if (icon == null) { // System.out.println("Getting icon of " + filename); icon = fsv.getSystemIcon(file); this.iconCache.put(filename, icon); } result.setIcon(icon); } return result; } } private static class FileTreeNode implements TreeNode { private File file; private File[] children; private TreeNode parent; private boolean isFileSystemRoot; public FileTreeNode(File file, boolean isFileSystemRoot, TreeNode parent) { this.file = file; this.isFileSystemRoot = isFileSystemRoot; this.parent = parent; this.children = this.file.listFiles(); if (this.children == null) this.children = new File[0]; } public FileTreeNode(File[] children) { this.file = null; this.parent = null; this.children = children; } public Enumeration<?> children() { final int elementCount = this.children.length; return new Enumeration<File>() { int count = 0; public boolean hasMoreElements() { return this.count < elementCount; } public File nextElement() { if (this.count < elementCount) { return FileTreeNode.this.children[this.count++]; } throw new NoSuchElementException("Vector Enumeration"); } }; } public boolean getAllowsChildren() { return true; } public TreeNode getChildAt(int childIndex) { return new FileTreeNode(this.children[childIndex], this.parent == null, this); } public int getChildCount() { return this.children.length; } public int getIndex(TreeNode node) { FileTreeNode ftn = (FileTreeNode) node; for (int i = 0; i < this.children.length; i++) { if (ftn.file.equals(this.children[i])) return i; } return -1; } public TreeNode getParent() { return this.parent; } public boolean isLeaf() { return (this.getChildCount() == 0); } } private JTree tree; public FileTreePanel() { this.setLayout(new BorderLayout()); File[] roots = File.listRoots(); FileTreeNode rootTreeNode = new FileTreeNode(roots); this.tree = new JTree(rootTreeNode); this.tree.setCellRenderer(new FileTreeCellRenderer()); this.tree.setRootVisible(false); final JScrollPane jsp = new JScrollPane(this.tree); jsp.setBorder(new EmptyBorder(0, 0, 0, 0)); this.add(jsp, BorderLayout.CENTER); } }
Comment