JavaFXTreeView目录列表
我正在尝试创建一个JavaFXTreeView目录列表,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我正在尝试创建一个TreeView,它显示目录内容,例如: ABC BCD 123.php 其中ABC和BCD都是目录。我觉得我遗漏了一些东西,因为在剥离完整目录位置之前,TreeView工作正常,但一旦剥离,它就不会像上面那样显示 public void displayTreeView(String inputDirectoryLocation, CheckBoxTreeItem<String> mainRootItem) { // Creates th
TreeView
,它显示目录内容,例如:
ABC
BCD
123.php
其中ABC和BCD都是目录。我觉得我遗漏了一些东西,因为在剥离完整目录位置之前,TreeView
工作正常,但一旦剥离,它就不会像上面那样显示
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);
}
}
public void displayTreeView(字符串inputDirectoryLocation,CheckBoxTreeItem mainRootItem){
//创建根项目。
CheckBoxTreeItem rootItem=新的CheckBoxTreeItem(inputDirectoryLocation);
//隐藏树状图的根项目。
treeView.setShowRoot(假);
//创建单元工厂。
setCellFactory(CheckBoxTreeCell.forTreeView());
//获取文件列表。
File fileInputDirectoryLocation=新文件(inputDirectoryLocation);
文件fileList[]=fileInputDirectoryLocation.listFiles();
//循环浏览文件列表中的每个文件和目录。
for(int i=0;i
通过将树视图的初始化与构建树的递归方法相结合,您创建了凌乱的代码
最好仅为创建树创建一个新方法:
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()));
}
}
顺便说一句:您的问题是由于创建树结构并忽略除根以外的每个节点(对于非根项目,唯一添加到rootItem
的节点;对于根以外的项目,您添加的是父项的“flat”dirCheckBoxTreeItem
).通过将树视图的初始化与构建树的递归方法相结合,您创建了凌乱的代码
最好仅为创建树创建一个新方法:
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()));
}
}
顺便说一句:您的问题是由于创建树结构并忽略除根以外的每个节点(对于非根项目,唯一添加到rootItem
的节点;对于根以外的项目,您添加的是父项的“flat”dirCheckBoxTreeItem
).很抱歉,我碰到了一个老问题,但我在实现fabian的答案时遇到了一个问题,我认为这可能是有用的
浏览大目录(例如根目录)可能会导致性能问题。我解决了这个问题,让它只在TreeItem
展开后才继续递归
private void createTree(文件根目录文件,树项父目录){
if(root_file.isDirectory()){
TreeItem节点=新的TreeItem(root_file.getName());
parent.getChildren().add(节点);
对于(文件f:root\u File.listFiles()){
TreeItem placeholder=new TreeItem();//添加TreeItem以使父级即使没有子级也可展开。
node.getChildren().add(占位符);
//展开父级时,继续递归
node.addEventHandler(TreeItem.BranchExPandeEvent(),new EventHandler()){
@凌驾
公共无效句柄(事件){
createTree(f,node);//像往常一样继续递归
node.getChildren().remove(占位符);//remove placeholder
node.removeEventHandler(TreeItem.BranchExPandeEvent(),this);//删除事件
}
});
}
}否则{
parent.getChildren();
}
}
除了修改
循环的。我使用了TreeItem
而不是CheckBoxTreeItem
和一些变量名,其余的都是相同的
我还遇到了另一个只读或受保护的Windows文件夹/文件问题,例如$RECYCLE.BIN
和系统卷信息
。我通过检查文件是系统文件还是隐藏文件或只读文件来解决问题。如果是,则忽略该文件/文件夹
试试看{
DosFileAttributes attr=Files.readAttributes(root_file.toPath(),DosFileAttributes.class);
if(attr.isSystem()| | attr.ishiden()| | attr.isReadOnly()){
//无所事事
}否则{…}
}捕获(IOEX异常){
Logger.getLogger(FXMLMainController.class.getName()).log(Level.SEVERE,null,ex);
}
很抱歉,我碰到了一个老问题,但我在实现fabian的答案时遇到了一个问题,我认为这可能是有用的
浏览大目录(例如根目录)可能会导致性能问题。我解决了这个问题,让它只在TreeItem
展开后才继续递归
private void createTree(文件根目录文件,树项父目录){
if(root_file.isDirectory()){
TreeItem节点=新的TreeItem(root_file.getName());
parent.getChildren().add(节点);
对于(文件f:root\u File.listFiles()){
TreeItem占位符=新建TreeItem();//添加Tr