package com.kevin.treedemo.filesystem;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;
public class FileTree extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JTree tree = null;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("D");
public FileTree() {
setLayout(new BorderLayout(0, 0));
tree = new JTree(root,true);
JScrollPane scrollPane = new JScrollPane(tree);
add(scrollPane, BorderLayout.CENTER);
initTree();
initTreeListener();
}
private void initTreeListener() {
tree.addTreeWillExpandListener(new TreeWillExpandListener(){
@Override
public void treeWillExpand(TreeExpansionEvent event)
throws ExpandVetoException {
TreePath o = event.getPath();
DefaultMutableTreeNode node = (DefaultMutableTreeNode)o.getLastPathComponent();
String file = node.toString();
String[] files = FiltUtil.listDir("D:\\"+file);
if(null == files){
return;
}
for(String f:files){
node.add(new DefaultMutableTreeNode(f,!FiltUtil.isFile("D:\\"+file+"\\"+f)));
}
//不能使用updateUI,否则会出现异常
/*Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTreeUI.updateSize(BasicTreeUI.java:1801)
at javax.swing.plaf.basic.BasicTreeUI.toggleExpandState(BasicTreeUI.java:2210)
at javax.swing.plaf.basic.BasicTreeUI.handleExpandControlClick(BasicTreeUI.java:2196)
at javax.swing.plaf.basic.BasicTreeUI.checkForClickInExpandControl(BasicTreeUI.java:2154)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3508)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3456)
at java.awt.Component.processMouseEvent(Component.java:6035)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3983)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
*/
//tree.updateUI();
}
@Override
public void treeWillCollapse(TreeExpansionEvent event)
throws ExpandVetoException {
}
});
}
private void initTree() {
String [] files = FiltUtil.listDir("D:\\");
for(String file:files){
root.add(new DefaultMutableTreeNode(file,!FiltUtil.isFile("D:\\"+file)));
}
tree.updateUI();
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setSize(500, 400);
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
f.setContentPane(new FileTree());
f.setVisible(true);
}
}
package com.kevin.treedemo.filesystem;
import java.io.File;
public class FiltUtil {
public static String[] listDir(String path){
File file = new File(path);
if(file.isDirectory()){
return file.list();
} else {
return null;
}
}
public static boolean isFile(String path){
File file = new File(path);
return file.isFile();
}
}