SWING?1ê??μí3???tê÷
ê±??:2011-08-11 ?ˉ?ò??3±
ò?°??é????£?11?¨ò??? SWING ê÷£???ê??èòa11?¨o?′?·?êy?Yμ??£Dí (TreeModel), ?úò?°??é????£?êμ ???eà′????óD?êìa£?μ?μ±êy?Yá?·?3£′óμ?ê±oò£?ò?′?D?11?¨o? TreeModel £????á?¨·?oü?àê±??£?????′| óú?òμ?μ?×′ì?£¨μ±è??éò?áí?a??????3ì£??éò?2ù×÷????????£?£??a????ó??§μ??D??·?3£2??£?a???a???êìa μ?1??ü?úóú£?ê÷?£Díμ?êy?Y?£?ò?????aμà£??μí3???tê?ò?2?ò?2?oüóD2?′?D?£??a?ù?ò???í?éò?ò?ò??????t £¨2?1üê????t?1ê????t?D£??aê÷?úμ?£?μ±3?′??ó??ê÷μ?ê±oò£?μ??÷ê÷?úμ?£??è?D????óD??óD×ó?úμ?£?è??T £??íè¥?áè¥???úμ?μ?±£′?μ????tD??¢£?è??a???t?D£??íè¥è??????t?D??μ??ùóD???t£?11?ì3?ê÷?úμ?£??óμ? ê÷?D?£μú?t′?μ??÷μ?ê±oò£??í2??áè¥?á′??ì???t?μí3á??£
′ú????μ¥£o
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.TreeCellRenderer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.tree.TreePath;
/**
* <p>Title: JDHSystemFileTree</p>
* <p>Description: ?μí3????ê÷£??ˉì?éú3é£??a????è??yμ??êìa </p>
* <p>Copyright: Copyright (c) 2007</p>
* @author ?ˉ?ò??3±
* @version 1.0
*/
public class SystemFileTree {
???? private DefaultTreeModel model;
???? private JTree tree;
???? public SystemFileTree () {
???????? JFrame f=new JFrame();
???????? f.getContentPane().setLayout(new BorderLayout());
???????? tree=new JTree();
?????? tree.addMouseListener(new MouseAdapter(){
???????????? public void mousePressed(MouseEvent e){
???????????????? node_mouseAction(e);
???????????? }
???????? });
???????? JScrollPane scroll=new JScrollPane(tree);
???????? f.getContentPane().add(scroll,BorderLayout.CENTER);
???????? f.setLocation(250,250);
???????? f.setSize(new Dimension(300,500));
???????? f.setVisible(true);
???? }
???? private void node_mouseAction(MouseEvent e){
???????? int row = tree.getRowForLocation(e.getX(), e.getY());
???????? PathNode pathNode =null;
???????? if(row != -1){
???????? TreePath path = tree.getPathForRow(row);
???????? pathNode = (PathNode)path.getLastPathComponent();
???????? if(pathNode.isFolder()&&pathNode.getChildCount()==0){
???????????? builderNode(pathNode);
???????????? tree.expandPath(path);
???????? }
???????? }
???? }
???? private PathNode builderNode(PathNode pathNode){
???????? String filePath= pathNode.getValue().toString();
???????? File file=new File(filePath);
???????? File[] files=file.listFiles();
?????? for(int i=0;i<files.length;i++){
?????????? PathNode node=new PathNode(files[i].getName(), files[i].getAbsolutePath (),files[i].isDirectory());
?????????? pathNode.add(node);
?????? }
???????? return pathNode;
???? }
???? private void initData(String??rootPath){
???????? Fil
|