sunfruit[请访问http://www.fruitres.cn]

--我相信JAVA能走得更远 QQ:316228067

[原创]JAVA编写的带有复选框的资源管理器树目录

    --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) {
  }
}

posted on 2006-02-19 17:59 sunfruit 阅读(2616) 评论(10)  编辑  收藏 所属分类: JAVA SE & EE

评论

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2006-02-19 21:22 大侠

不错啊,呵呵  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2006-03-20 13:27 jack wang

非常好!!
如果此樹狀可以點選到檔案而非到資料夾更好.  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2006-03-20 14:55 sun fruit

--sunfruit
多谢jack wang 的意见
原来这个树目录是用在资源管理器里面的,文件显示在右面的JTable组件里面,所以就没有显示到文件  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2006-10-27 14:45 吴[匿名]

非常好,楼猪能告诉我怎么写一个有两级目录的自己定义的树么?  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2006-11-20 14:59 jack[匿名]

楼主,那右边显示文件的表格可否一看呢,表格的选择模式是单列,还有文件图标的显示和名称的显示怎么来处理呢? 谢谢.
E_mail: ccnujzw@163.com  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2006-12-13 12:45 sunfruit[匿名]

你添加我的联系方式,我给你相关的代码参考  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2007-12-29 15:11 snowman

怎么没有main(0  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2007-12-30 12:00 fruit

这个是组件,不需要main函数  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2008-05-01 12:59 wang

怎么调用?  回复  更多评论   

# re: [原创]JAVA编写的带有复选框的资源管理器树目录 2008-09-27 17:07 yingchun

很好.  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: