我正在使用以下代码来填充一个运行良好的 JTree
File [] children = new File(directory).listFiles(); // list all the files in the directory
for (int i = 0; i < children.length; i++) { // loop through each
DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName());
// only display the node if it isn't a folder, and if this is a recursive call
if (children[i].isDirectory() && recursive) {
parent.add(node); // add as a child node
listAllFiles(children[i].getPath(), node, recursive); // call again for the subdirectory
} else if (!children[i].isDirectory()){ // otherwise, if it isn't a directory
parent.add(node); // add it as a node and do nothing else
}
}
给出一个目录字符串,它将该目录中的所有文件添加到 JTree,我的问题是我无法从每个节点获取文件......我知道你可以使用
jtree.getLastSelectedPathComponent()
获取最后一个选择的组件,但我真正要检查的是选择的组件是否属于类型File
,如果是,则返回该文件的路径...有人知道该怎么做吗?