📄 jfiletree.java
字号:
package com.sunking.swing;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.filechooser.*;
import javax.swing.tree.TreePath;
import java.io.File;
import java.util.*;
/**
* <p>Title: OpenSwing</p>
* <p>Description: JFileTree 文件树</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
* @version 1.0
*/
public class JFileTree
extends JTree implements Serializable {
public static final String SINGLE_FILE_MODEL = "SINGLE_FILE_MODEL";
public static final FileSystemView fileSystemView = FileSystemView.
getFileSystemView();
DefaultTreeModel treeModel;
public JFileTree() {
treeModel = (DefaultTreeModel) UIManager.get(SINGLE_FILE_MODEL);
if (treeModel == null) {
FileNode root = new FileNode(fileSystemView.getRoots()[0]);
treeModel = new DefaultTreeModel(root);
( (FileNode) treeModel.getRoot()).explore();
UIManager.put(SINGLE_FILE_MODEL, treeModel);
}
this.setModel(treeModel);
addTreeExpansionListener(new JFileTreeExpandsionListener());
setCellRenderer(new JFileTreeCellRenderer());
}
public FileNode getSelectFileNode() {
TreePath path = getSelectionPath();
if (path == null || path.getLastPathComponent() == null) {
return null;
}
return (FileNode) path.getLastPathComponent();
}
public void setSelectFileNode(FileNode f) throws Exception {
this.setSelectFile(f.getFile());
}
public File getSelectFile() {
FileNode node = getSelectFileNode();
return node == null ? null : node.getFile();
}
public void setSelectFile(File f) throws Exception {
FileNode node = this.expandFile(f);
TreePath path = new TreePath(node.getPath());
this.scrollPathToVisible(path);
this.setSelectionPath(path);
this.repaint();
}
public FileNode expandFile(File f) throws Exception {
if (!f.exists()) {
throw new java.io.FileNotFoundException(f.getAbsolutePath());
}
Vector vTemp = new Vector();
File fTemp = f;
while (fTemp != null) {
vTemp.add(fTemp);
fTemp = fileSystemView.getParentDirectory(fTemp);
}
FileNode nParent = (FileNode) treeModel.getRoot();
for (int i = vTemp.size() - 1; i >= 0; i--) {
fTemp = (File) vTemp.get(i);
nParent.explore();
for (int j = 0; j < nParent.getChildCount(); j++) {
FileNode nChild = (FileNode) nParent.getChildAt(j);
if (nChild.getFile().equals(fTemp)) {
nParent = nChild;
}
}
}
return nParent;
}
class JFileTreeCellRenderer
extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,
hasFocus);
try {
FileNode node = (FileNode) value;
closedIcon = fileSystemView.getSystemIcon( ( (FileNode) value).getFile());
openIcon = closedIcon;
setIcon(closedIcon);
}
catch (Exception ex) {
}
return this;
}
}
class JFileTreeExpandsionListener
implements TreeExpansionListener {
public JFileTreeExpandsionListener() {}
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
if (path == null || path.getLastPathComponent() == null)
return;
setCursor(new Cursor(Cursor.WAIT_CURSOR));
FileNode node = (FileNode) path.getLastPathComponent();
node.explore();
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void treeCollapsed(TreeExpansionEvent event) {}
}
public static class FileNode
extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) {
setUserObject(file);
}
public boolean getAllowsChildren() {
return isDirectory();
}
public boolean isDirectory() {
return !isLeaf();
}
public boolean isLeaf() {
return false;
}
public File getFile() {
return (File) getUserObject();
}
public boolean isExplored() {
return explored;
}
public void setExplored(boolean b) {
explored = b;
}
public String toString() {
if (getFile() instanceof File)
return fileSystemView.getSystemDisplayName( (File) getFile());
else
return getFile().toString();
}
public void explore() {
if (!explored) {
explored = true;
File file = getFile();
File[] children = file.listFiles();
if (children == null || children.length == 0) {
return;
}
for (int i = 0; i < children.length; ++i) {
File f = children[i];
if (f.isDirectory()) {
add(new FileNode(f));
}
}
DefaultTreeModel model = (DefaultTreeModel) UIManager.get(SINGLE_FILE_MODEL);
if(model != null){
model.nodeStructureChanged(this);
}
}
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
}
JFrame frame = new JFrame();
JFileTree tree = new JFileTree();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("JFileTree Demo");
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(
new JScrollPane(tree), BorderLayout.CENTER);
frame.setSize(400, 320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.setVisible(true);
try {
tree.setSelectFile(new File("C:/"));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -