次のコードを使用して、完全に機能している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
であるかどうかを確認し、そうである場合はそのファイルのパスを返します...これを行う方法を知っている人はいますか?