--sunfruit
用JAVA实现了带有复选框的树目录
JDK版本
JDK1.4.x
功能
实现了带有复选框的资源管理器树目录,还有需要改进的地方,我在以后更新,如果那位朋友有好的建议欢迎提出
欢迎大家提意见,交流
代码如下
import javax.swing.tree.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.awt.Cursor;
import java.awt.Component;
import java.awt.Font;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
/**
* Title: 系统级树目录
* Description:
* Copyright: Copyright (c) 2004
* Company:
* @author cuijiang contact cj0063@sina.com or cuij7718@yahoo.com.cn
* @version 1.0
*/
public class AgileSuperJTreeBasic
extends JTree
implements TreeExpansionListener, TreeSelectionListener, MouseListener {
protected DefaultTreeModel treeModel;
protected FileSystemView fileSystemView; //建立文件系统视类对象
protected FileNode root;
public AgileSuperJTreeBasic() {
Font myFont = new Font("宋体", 11, 12);
fileSystemView = FileSystemView.getFileSystemView();
root = new FileNode(fileSystemView.getRoots()[0]);
root.explore();
treeModel = new DefaultTreeModel(root);
this.setModel(treeModel); //设定树形菜单
this.addTreeExpansionListener(this); //打开/关闭节点事件
this.addTreeSelectionListener(this); //选择的事件
this.setCellRenderer(new MyTreeCellRenderer()); //生成图标
this.setFont(myFont);
this.setRootVisible(true);
this.setRowHeight(18);
this.addMouseListener(this);
}
//图标生成类
protected class MyTreeCellRenderer
extends JPanel
implements TreeCellRenderer {
JCheckBox check = new JCheckBox();
BorderLayout borderLayout1 = new BorderLayout();
JLabel label = new JLabel();
public MyTreeCellRenderer() {
this.setLayout(null);
this.add(check);
this.add(label);
check.setBackground(UIManager.getColor("Tree.textBackground"));
label.setBackground(UIManager.getColor("Tree.textBackground"));
this.setBackground(UIManager.getColor("Tree.textBackground"));
}
public Dimension getPreferredSize() {
Dimension checkDimension = check.getPreferredSize();
Dimension labelDimension = label.getPreferredSize();
return new Dimension(checkDimension.width + labelDimension.width,
(checkDimension.height < labelDimension.height ?
labelDimension.height : checkDimension.height));
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
String stringValue = tree.convertValueToText(value, sel, expanded, leaf,
row, hasFocus);
setEnabled(tree.isEnabled());
label.setFont(tree.getFont());
check.setSelected( ( (FileNode) value).isSelected());
//设置图标为系统的文件类型图标
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
label.setIcon(fileSystemView.getSystemIcon( ( (FileNode) value).getFile()));
label.setText(stringValue);
return this;
}
public void doLayout() {
Dimension checkDimension = check.getPreferredSize();
Dimension labelDimension = label.getPreferredSize();
int checkY = 0;
int labelY = 0;
if (checkDimension.height > labelDimension.height) {
labelY = (checkDimension.height - labelDimension.height) / 2;
}
else {
checkY = (labelDimension.height - checkDimension.height) / 2;
}
check.setLocation(0, checkY);
check.setBounds(0, checkY, checkDimension.width, checkDimension.height);
label.setLocation(checkDimension.width, labelY);
label.setBounds(checkDimension.width, labelY, labelDimension.width,
labelDimension.height);
}
}
//节点张开事件
public void treeExpanded(TreeExpansionEvent event) {
//判断是否是叶节点
//if (this.getLastSelectedPathComponent() == null) {
//System.out.println("ok");
//return;
//}
setCursor(new Cursor(Cursor.WAIT_CURSOR));
TreePath path = event.getPath();
//System.out.println(path.toString());
FileNode node = (FileNode) path.getLastPathComponent();
node.explore();
treeModel.nodeStructureChanged(node);
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
//节点闭合事件
public void treeCollapsed(TreeExpansionEvent event) {
}
//文件节点类
protected class FileNode
extends DefaultMutableTreeNode {
private boolean isSelected = false;
private boolean explored = false;
public FileNode(File file) {
this(file, false);
}
public FileNode(File file, boolean bool) {
super(file);
this.isSelected = bool;
}
//
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
if (children != null) {
Enumeration enum = children.elements();
while (enum.hasMoreElements()) {
FileNode node = (FileNode) enum.nextElement();
node.setSelected(isSelected);
}
}
}
//
public boolean getAllowsChildren() {
return isDirectory();
}
public boolean isLeaf() {
return!isDirectory();
}
public File getFile() {
return (File) getUserObject();
}
public boolean isExplored() {
return explored;
}
public void setExplored(boolean b) {
explored = b;
}
public boolean isDirectory() {
return getFile().isDirectory();
}
public String toString() {
File file = (File) getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length() - 1)
? filename.substring(index + 1) : filename;
}
public void explore() {
if (!isExplored()) {
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(children[i], isSelected));
}
explored = true;
}
}
}
/**
* 选择节点触发的事件
* 继承或是直接引用需要重新写此方法
* @param e
*/
public void valueChanged(TreeSelectionEvent e) {
//文件路径
String sFilePath = "";
Object myobj = this.getLastSelectedPathComponent();
if (myobj != null) {
sFilePath = ( (File) ( ( (DefaultMutableTreeNode) (myobj)).getUserObject())).
getPath();
}
//System.out.println(sFilePath);
}
/**
* Invoked when the mouse button has been clicked (pressed and released) on a
* component.
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseClicked(MouseEvent e) {
int count = e.getClickCount();
if (count != 1) {
//System.out.println(count);
}
else {
int x = e.getX();
int y = e.getY();
int row = this.getRowForLocation(x, y);
TreePath path = this.getPathForRow(row);
if (path != null) {
FileNode node = (FileNode) path.getLastPathComponent();
boolean isSelected = ! (node.isSelected());
node.setSelected(isSelected);
( (DefaultTreeModel)this.getModel()).nodeChanged(node);
}
}
}
/**
* Invoked when a mouse button has been pressed on a component.
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mousePressed(MouseEvent e) {
}
/**
* Invoked when a mouse button has been released on a component.
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseReleased(MouseEvent e) {
}
/**
* Invoked when the mouse enters a component.
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseEntered(MouseEvent e) {
}
/**
* Invoked when the mouse exits a component.
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseExited(MouseEvent e) {
}
}