学习Java的Swing的时候写的一个超简单文件浏览器
效果如图:
项目结构:
这里面主要用了两个控件,JTree和JTable
下面先说下左侧的文件树如何实现:
首先是FileTree类,继承于JTree类,这里实现了树节点展开,和鼠标移动事件响应
package FileTree;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File; import javax.swing.JTree; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeWillExpandListener; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.ExpandVetoException; import javax.swing.tree.TreePath; /** * Created by Administrator on 2016/3/29. */ public class FileTree extends JTree { private static final long serialVersionUID = 1L; public TreePath mouseInPath; static protected FileSystemView fileSystemView = FileSystemView.getFileSystemView(); public FileTree() { setRootVisible(false); addTreeWillExpandListener(new TreeWillExpandListener(){ @Override public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException { FileTreeNode fileNode = (FileTreeNode)event.getPath().getLastPathComponent(); if (!fileNode.isInit()){ File[] files; if(fileNode.isDummyRoot()) { files = fileSystemView.getRoots(); } else { files = fileSystemView.getFiles(fileNode.getFile(), false); } for (int i = 0; i < files.length; i++){ if (files[i].isDirectory()) { FileTreeNode childFileNode = new FileTreeNode(files[i]); fileNode.add(childFileNode); } } } fileNode.setInit(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; } } else if (mouseInPath != null) { Rectangle oldRect =getPathBounds(mouseInPath); mouseInPath = null; repaint(oldRect); } } }); } }
FileTreeCellRenderer继承自DefaultTreeCellRenderer, 负责树节点显示渲染,这里主要是需要重载getTreeCellRendererComponent()方法
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
解释:配置基于传入组件的渲染器。使用 convertValueToText
向树传递消息,从而设置该值,最终对 value
调用 toString
。基于选择设置前景色,基于 leaf
和 expanded
参数设置图标。
返回:渲染器用来绘制值的 Component
package FileTree;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import javax.swing.JLabel; import javax.swing.JTree; import javax.swing.plaf.ColorUIResource; import javax.swing.tree.DefaultTreeCellRenderer; /** * Created by Administrator on 2016/3/29. */ public class FileTreeCellRenderer extends DefaultTreeCellRenderer { private static final long serialVersionUID = 1L; public FileTreeCellRenderer(){ } @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { FileTree fileTree = (FileTree) tree; FileTreeNode fileNode = (FileTreeNode) value; File file = (File) fileNode.getUserObject(); JLabel label = (JLabel)super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus); label.setText(fileNode.getFileName()); label.setIcon(fileNode.getFileIcon()); label.setOpaque(false); if (fileTree.mouseInPath != null && fileTree.mouseInPath.getLastPathComponent().equals(value)) { label.setOpaque(true); // label.setBackground(new Color(255, 0, 0, 90)); label.setBackground(Color.gray); } return label; } @Override public void setBackground(Color bg) { if (bg instanceof ColorUIResource){ bg = null; } super.setBackground(bg); } }
FileTreeNode继承DefaultMutableTreeNode类, 用于保存文件名和其他文件相关属性
package FileTree;
import java.io.File;
import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode; /** * Created by Administrator on 2016/3/29. */ public class FileTreeNode extends DefaultMutableTreeNode { private static final long serialVersionUID = 1L; static FileSystemView fileSystemView = FileSystemView.getFileSystemView(); private boolean isInit; private boolean isDummyRoot; private String fileName; private Icon fileIcon; public File getFile() { return file; } private File file; public FileTreeNode(File file){ this(file, false, true); } public FileTreeNode(File file, boolean isDummyRoot){ this(file,isDummyRoot, true); } public FileTreeNode(File file, boolean isDummyRoot, boolean allowsChildren){ super(file, allowsChildren); this.file = file; this.fileName = fileSystemView.getSystemDisplayName(file); this.fileIcon = fileSystemView.getSystemIcon(file); this.isDummyRoot = isDummyRoot; } public boolean isInit() { return isInit; } public void setInit(boolean init) { isInit = init; } public boolean isDummyRoot() { return isDummyRoot; } public void setDummyRoot(boolean dummyRoot) { isDummyRoot = dummyRoot; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public Icon getFileIcon() { return fileIcon; } public void setFileIcon(Icon fileIcon) { this.fileIcon = fileIcon; } }
本文链接: http://www.bugcoding.com/entry/15
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。