Vincent.Chan‘s Blog

          常用鏈接

          統(tǒng)計

          積分與排名

          網(wǎng)站

          最新評論

          [原創(chuàng)]JAVA編寫的帶有復(fù)選框的資源管理器樹目錄

              --sunfruit

              用JAVA實現(xiàn)了帶有復(fù)選框的樹目錄


              JDK版本
                  JDK1.4.x
              功能
                  實現(xiàn)了帶有復(fù)選框的資源管理器樹目錄,還有需要改進的地方,我在以后更新,如果那位朋友有好的建議歡迎提出
             
              歡迎大家提意見,交流
             
              代碼如下
             
          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: 系統(tǒng)級樹目錄
           * 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; //建立文件系統(tǒng)視類對象
            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); //設(shè)定樹形菜單
              this.addTreeExpansionListener(this); //打開/關(guān)閉節(jié)點事件
              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());
                //設(shè)置圖標為系統(tǒng)的文件類型圖標
                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);
              }

            }

            //節(jié)點張開事件
            public void treeExpanded(TreeExpansionEvent event) {
              //判斷是否是葉節(jié)點
              //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));

            }

            //節(jié)點閉合事件
            public void treeCollapsed(TreeExpansionEvent event) {

            }

            //文件節(jié)點類
            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;
                }
              }

            }

            /**
             * 選擇節(jié)點觸發(fā)的事件
             * 繼承或是直接引用需要重新寫此方法
             * @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 14:36 Vincent.Chen 閱讀(882) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 长垣县| 宁蒗| 成都市| 荥阳市| 金阳县| 乌兰浩特市| 三亚市| 东乡| 宜丰县| 乐陵市| 屯门区| 保山市| 吉水县| 屯昌县| 普格县| 罗江县| 菏泽市| 页游| 额敏县| 五家渠市| 丹棱县| 天峻县| 汝阳县| 北宁市| 府谷县| 田东县| 仪征市| 锡林浩特市| 三河市| 岳阳市| 锦州市| 饶平县| 大城县| 长乐市| 筠连县| 威信县| 南华县| 石城县| 遂川县| 长武县| 睢宁县|