Jtree生成系统文件树
来源:互联网 发布:网络鲜花店 上海 编辑:程序博客网 时间:2024/06/05 02:17
Jtree原始的默认结点只具有虚拟意义,需要自定义文件结点代替虚拟结点,然后进行生成
class FileNode{
public FileNode(String name,Icon icon,File file,boolean isDummyRoot){
this.name=name;this.icon=icon;this.file=file;
this.isDummyRoot=isDummyRoot;
}
public boolean isInit;
public boolean isDummyRoot;
public String name;
public Icon icon;
public File file;
}
每次添加文件结点是,先用自定义的文件结点初始化虚拟结点,进行树状结构的添加,从而实现文件与结点 的相关联
FileNode childFileNode = new FileNode(files[i].getName(),getIcon(files[i]),files[i], false);
DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(childFileNode);
lastTreeNode.add(childTreeNode);
下面将生成系统文件树
class FileTree extends JTree {
public TreePath mouseInPath;
protected FileSystemView fileSystemView = FileSystemView.getFileSystemView();
public FileTree(){
setRootVisible(false);
addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
DefaultMutableTreeNode lastTreeNode =(DefaultMutableTreeNode) event.getPath().getLastPathComponent();
FileNode fileNode = (FileNode) lastTreeNode.getUserObject();
if (!fileNode.isInit) {
File[] files;
if (fileNode.isDummyRoot) {
files = fileSystemView.getRoots();
} else {
files = fileSystemView.getFiles(
((FileNode) lastTreeNode.getUserObject()).file,
false);
}
for (int i = 0; i < files.length; i++) {
FileNode childFileNode = new FileNode(
fileSystemView.getSystemDisplayName(files[i]),
fileSystemView.getSystemIcon(files[i]), files[i],
false);
DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(childFileNode);
lastTreeNode.add(childTreeNode);
}
DefaultTreeModel treeModel1 = (DefaultTreeModel) getModel();
treeModel1.nodeStructureChanged(lastTreeNode);
}
fileNode.isInit = true;
}
@Override
public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
TreePath path=getPathForLocation(e.getX(), e.getY());
if(path!=null){
if(mouseInPath!=null){
Rectangle oldRect=getPathBounds(mouseInPath);
mouseInPath=path;
repaint(getPathBounds(path).union(oldRect));
}else{
mouseInPath=path;
Rectangle bounds=getPathBounds(mouseInPath);
repaint(bounds);
}
}else if(mouseInPath!=null){
Rectangle oldRect=getPathBounds(mouseInPath);
mouseInPath=null;
repaint(oldRect);
}
}
});
}
}
- Jtree生成系统文件树
- JTree实现的系统文件目录树
- JTree实现的系统文件目录树
- JTree实现文件树
- JTree实现文件树——使用系统图标
- 【JAVA】JTree遍历指定目录生成树
- jTree打造硬盘文件目录树
- JTree文件树相关操作--学习推荐
- JTree树转存为xml文件
- JTree使用之调用数据库动态生成JTree
- JTree--树(基本使用)
- jtree
- JTree
- JTree
- JTree
- JTree
- JTree
- JTree
- MyEclipse创建Maven项目提示缺少maven-resources-plugin:2.6
- Activity touch事件传递流程分析
- 8大你不得不知的Android调试工具
- 561. Array Partition I
- QQ-AR实物识别!香蕉扫一扫,解救小黄人?
- Jtree生成系统文件树
- 华为——求int型正整数在内存中存储时1的个数
- ArrList详细讲述
- 线段树复习--[kuangbin带你飞] 线段树
- TCP UDP
- 浮动 清除浮动
- Glog 介绍
- activemq 事务--遇到异常始终回滚
- java内存机制