我使用下面的代碼來填充一個JTree這是工作完美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
,如果是,則返回該文件的路徑...是否有人知道如何做到這一點?
+1(both)抱歉(愚蠢的提示)我錯過了Colection.sort() – mKorbel 2012-07-10 20:19:52