我想在treeselectionlistner上获取文件夹的路径。任何人都可以帮助我

时间:2016-07-26 13:02:18

标签: java jtree

我想在treeselectionlistner上获取文件夹的路径。谁能帮我 。我想要在JTree中选择文件夹的确切路径。

// 1班

公共类FileBrowser实现Runnable {

private DefaultMutableTreeNode root;

private DefaultTreeModel treeModel;

private JTree tree;

@Override
public void run() {
    JFrame frame = new JFrame("File Browser");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    File fileRoot = new File("C:\\JDeveloper\\Testing");
    root = new DefaultMutableTreeNode(new FileNodes(fileRoot));
    treeModel = new DefaultTreeModel(root);

    tree = new JTree(treeModel);
    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon");
        private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon");
        @Override
        public Component getTreeCellRendererComponent( JTree tree,
                                                       Object value,
                                                       boolean bSelected,
                                                       boolean bExpanded,
                                                       boolean bLeaf,
                                                       int iRow,
                                                       boolean bHasFocus ) {
            Component c = super.getTreeCellRendererComponent(tree, value,
                    bSelected, bExpanded, false, iRow, bHasFocus);

            return c;
        }
    });
    tree.setShowsRootHandles(true);
    tree.addTreeSelectionListener(new TreeSelectionListener() {
         public void valueChanged(TreeSelectionEvent e) {
           DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
               .getPath().getLastPathComponent();
           System.out.println("You selected " +node);
         }
       });
    JScrollPane scrollPane = new JScrollPane(tree);

    frame.add(scrollPane);
    frame.setLocationByPlatform(true);
    frame.setSize(640, 480);
    frame.setVisible(true);

    CreateChildNode ccn = new CreateChildNode(fileRoot, root);
    new Thread(ccn).start();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new FileBrowser());
}

}

类CreateChildNode实现Runnable {

private DefaultMutableTreeNode root;

private File currentFile;

public CreateChildNode(File currentFile, DefaultMutableTreeNode root) {
    this.currentFile = currentFile;
    this.root = root;
}

@Override
public void run() {
    createChildren(currentFile, root);
}

private void createChildren(File currentFile, DefaultMutableTreeNode node)
{
    File[] files = currentFile.listFiles();
    if ( files != null )
    {
        for( File file : files )
        {
            DefaultMutableTreeNode childNode = new DefaultMutableTreeNode( new FileNodes( file ) );
            if( file.isDirectory() )
            {
                node.add( childNode );
                createChildren( file, childNode );
            }
        }
    }
}

}

class FileNodes {

private File file;

public FileNodes(File file) {
    this.file = file;
}
public File getFile() {
    return file;
}

@Override
public String toString() {
    String name = file.getName();
    if (name.equals("")) {
        return file.getAbsolutePath();
    } else {
        return name;
    }
}

}

2 个答案:

答案 0 :(得分:0)

更改

DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
           .getPath().getLastPathComponent();

TreePath node = (TreePath) e
           .getPath();

目前你告诉它只给你路径的尽头,我猜你想要整个事情。

要获取它引用的文件,请使用以下代码:

String path = node.toString().replaceAll("\\]| |\\[|", "").replaceAll(",", File.separator);
File f = new File(path);

来自这个答案:TreePath to java.io.File

答案 1 :(得分:0)

请按以下步骤更新TreeSelectionListener valueChanged()方法

tree.addTreeSelectionListener(new TreeSelectionListener() {
    public void valueChanged(TreeSelectionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
        FileNodes fileNode = (FileNodes)node.getUserObject();
        System.out.println("You selected " + fileNode.getFile().getAbsolutePath());
    }
});