java swing 路径选择_swing的路径选择

展开全部

public class FileTree extends JTree {

public FileTree(String path)

throws FileNotFoundException, SecurityException {

super((TreeModel) null);// Create the JTree itself

// Use horizontal and vertical lines

putClientProperty("JTree.lineStyle", "Angled");

// Create the first node

FileTreeNode rootNode = new FileTreeNode(null, path);

// Populate the root node with its subdirectories

boolean addedNodes = rootNode.populateDirectories(true);

setModel(new DefaultTreeModel(rootNode));

// Listen for Tree Selection Events

addTreeExpansionListener(new TreeExpansionHandler());

}

// Returns the full pathname for a path, or null

// if not a known path

public String getPathName(TreePath path) {

Object o = path.getLastPathComponent();

if (o instanceof FileTreeNode) {

return ((FileTreeNode) o).file.getAbsolutePath();

}

return null;

}

// Returns the File for a path, or null if not a known path

public File getFile(TreePath path) {

Object o = path.getLastPathComponent();

if (o instanceof FileTreeNode) {

return ((FileTreeNode) o).file;

}

return null;

}

// Inner class that represents a node in this

// file system tree

protected static class FileTreeNode

extends DefaultMutableTreeNode {

public FileTreeNode(File parent, String name)

throws SecurityException, FileNotFoundException {

this.name = name;

// See if this node exists and whether it

// is a directory

file = new File(parent, name);

if (!file.exists()) {

throw new FileNotFoundException("File " + name + " does not exist");

}

isDir = file.isDirectory();

// Hold the File as the user object.

setUserObject(file);

}

// Override isLeaf to check whether this is a directory

public boolean isLeaf() {

return !isDir;

}

// Override getAllowsChildren to check whether

// this is a directory

public boolean getAllowsChildren() {

return isDir;

}

// For display purposes, we return our own name public String toString() { return name; }

// If we are a directory, scan our contents and populate

// with children. In addition, populate those children

// if the "descend" flag is true. We only descend once,

// to avoid recursing the whole subtree.

// Returns true if some nodes were added

boolean populateDirectories(boolean descend) {

boolean addedNodes = false;

// Do this only once

if (populated == false) {

if (interim == true) {

// We have had a quick look here before:

// remove the dummy node that we added last time

removeAllChildren();

interim = false;

}

String[] names = file.list();// Get list of contents

// Process the directories

for (int i = 0; i < names.length; i++) {

String name = names[i];

File d = new File(file, name);

try {

if (d.isDirectory()) {

FileTreeNode node =

new FileTreeNode(file, name);

this.add(node);

if (descend) {

node.populateDirectories(false);

}

addedNodes = true;

if (descend == false) {

// Only add one node if not descending

break;

}

}

} catch (Throwable t) {

// Ignore phantoms or access problems

}

}

// If we were scanning to get all subdirectories,

// or if we found no subdirectories, there is no

// reason to look at this directory again, so

// set populated to true. Otherwise, we set interim

// so that we look again in the future if we need to

if (descend == true || addedNodes == false) {

populated = true;

} else {

// Just set interim state

interim = true;

}

}

return addedNodes;

}

protected File file;// File object for this node

protected String name;// Name of this node

protected boolean populated;

// true if we have been populated

protected boolean interim;

// true if we are in interim state

protected boolean isDir;// true if this is a directory

}

// Inner class that handles Tree Expansion Events

protected class TreeExpansionHandler

implements TreeExpansionListener {

public void treeExpanded(TreeExpansionEvent evt) {

TreePath path = evt.getPath();// The expanded path JTree tree =

JTree tree = (JTree) evt.getSource();// The tree

// Get the last component of the path and

// arrange to have it fully populated.

FileTreeNode node =

(FileTreeNode) path.getLastPathComponent();

if (node.populateDirectories(true)) {

((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node);

}

}

public void treeCollapsed(TreeExpansionEvent evt) {

// Nothing to do

}

}

public static void main(String[] args) {

JFrame frame = new JFrame();

try {

FileTree tree = new FileTree("d:");

frame.getContentPane().add(tree);

frame.setDefaultCloseOperation(3);

frame.setVisible(true);

} catch (FileNotFoundException ex) {

Logger.getLogger(FileTree.class.getName()).log(Level.SEVERE, null, ex);

} catch (SecurityException ex) {

Logger.getLogger(FileTree.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

你可以改变显示的根目e68a8462616964757a686964616f31333239303934录

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值