我正在尝试获取 treeselectionlistner 上的文件夹路径。谁能帮我

I am trying to get the path of folder on treeselectionlistner . Can anyone help me

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

//Class 1

public class 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());
}

}

class 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 文件节点 {

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;
    }
}

}

改变

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

请更新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());
    }
});