Javafx TreeView目录列表 [英] Javafx TreeView Directory Listing
问题描述
我正在尝试创建一个 TreeView
,它显示目录内容,例如:
I'm trying to create a TreeView
which displays directory contents such as:
ABC
BCD
123.php
ABC和BCD都是目录。我觉得我错过了一些东西,因为 TreeView
在删除完整目录位置之前工作正常但是一旦我剥离它,它就不会像上面那样显示。 / p>
Where ABC and BCD are both directories. I feel like I'm missing something as the TreeView
works fine before I strip out the full directory location but once I strip it, it won't display like the above.
public void displayTreeView(String inputDirectoryLocation, CheckBoxTreeItem<String> mainRootItem) {
// Creates the root item.
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>(inputDirectoryLocation);
// Hides the root item of the tree view.
treeView.setShowRoot(false);
// Creates the cell factory.
treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
// Get a list of files.
File fileInputDirectoryLocation = new File(inputDirectoryLocation);
File fileList[] = fileInputDirectoryLocation.listFiles();
// Loop through each file and directory in the fileList.
for (int i = 0; i < fileList.length; i++) {
// Check if fileList[i] is a file or a directory.
if (fileList[i].isDirectory()) {
// Re-iterate through as is directory.
displayTreeView(fileList[i].toString(), rootItem);
} else {
// Check the file type of the file.
String fileType = Util.retrieveFileType(fileList[i].toString());
// Check if the file type is the same file type we are searching for. In the future we just add the or symbol to support other file types.
if (fileType.equals(".php")) {
// Creates the item.
CheckBoxTreeItem<String> fileCheckBoxTreeItem = new CheckBoxTreeItem<>(fileList[i].getName());
// Adds to the treeview.
rootItem.getChildren().add(fileCheckBoxTreeItem);
}
}
}
// Check if the mainRootItem has been specified.
if (mainRootItem == null) {
// Sets the tree view root item.
treeView.setRoot(rootItem);
} else {
// Creates the root item.
CheckBoxTreeItem<String> dirCheckBoxTreeItem = new CheckBoxTreeItem<>(fileInputDirectoryLocation.getName());
// Sets the sub-root item.
mainRootItem.getChildren().add(dirCheckBoxTreeItem);
}
}
推荐答案
By结合 TreeView
的初始化和用于构造树的递归方法,您创建了混乱的代码。
By combining the initialisation of the TreeView
and a recursive method for constructing the tree you created messy code.
更好地创建一个用于创建树的新方法:
Better create a new method just for creating the tree:
public static void createTree(File file, CheckBoxTreeItem<String> parent) {
if (file.isDirectory()) {
CheckBoxTreeItem<String> treeItem = new CheckBoxTreeItem<>(file.getName());
parent.getChildren().add(treeItem);
for (File f : file.listFiles()) {
createTree(f, treeItem);
}
} else if (".php".equals(Util.retrieveFileType(file.toString()))) {
parent.getChildren().add(new CheckBoxTreeItem<>(file.getName()));
}
}
并在中使用它displayTreeView
method
public void displayTreeView(String inputDirectoryLocation) {
// Creates the root item.
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>(inputDirectoryLocation);
// Hides the root item of the tree view.
treeView.setShowRoot(false);
// Creates the cell factory.
treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
// Get a list of files.
File fileInputDirectoryLocation = new File(inputDirectoryLocation);
File fileList[] = fileInputDirectoryLocation.listFiles();
// create tree
for (File file : fileList) {
createTree(file, rootItem);
}
treeView.setRoot(rootItem);
}
BTW:你的问题是通过创建树结构并忽略它为每个节点而不是根节点(对于非根项目,唯一节点添加到 rootItem
;对于您添加的根以外的项目是而平面 dirCheckBoxTreeItem
代替父母。)
BTW: Your issue is caused by creating the tree structure and ignoring it for every node but the root (for non-root items the only node added to rootItem
; for items other than the root you add is the "flat" dirCheckBoxTreeItem
to the parent instead).
这篇关于Javafx TreeView目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!