ExplorerApplet.java
/** *This is a simple Windows File System Explorer program written in java. *@author Tapas kumar jena *@mail tapas.friends@gmail.com */ import java.awt.Component; import java.io.File; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; import javax.swing.Icon; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.UIManager; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreeNode; /* * This prograam will read your local file system and show you in a tree structure. */ public class ExplorerApplet extends JApplet { File[] roots; FileTreeNode rootTreeNode; private JTree tree; protected static FileSystemView fsv = FileSystemView.getFileSystemView(); public void init() { try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch (Exception e) { e.printStackTrace(); } roots = File.listRoots(); rootTreeNode = new FileTreeNode(roots); tree = new JTree(rootTreeNode); tree.setCellRenderer(new FileTreeCellRenderer()); tree.setName("My Computer"); tree.setRootVisible(true); final JScrollPane jsp = new JScrollPane(tree); add(jsp); } private static class FileTreeCellRenderer extends DefaultTreeCellRenderer { private Map iconCache = new HashMap(); private Map rootNameCache = new HashMap(); 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 = (String) 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 = (Icon)this.iconCache.get(filename); if (icon == null) { 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() { 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); } } }The above prograam looks like below -