所以我有一个获取所有文件和文件夹的类,并将它们作为字符串数组返回。这是输出。
/applications/HI
/applications/HI/Hey.txt
/applications/HI/Milk
/applications/HI/Milk/blah
/applications/HI/Milk/HEADIAJQ/text.txt
/applications/HI/Milk/HEADIAJQ/thisworks!!!!.txt
/applications/HI/MilkiM/TRASHINGTHINGS.txt
/applications/HI/emoji.txt
/applications/Test
/applications/Test/Tjinsg
所以从这里开始,我有点卡住了。我不知道如何从javafx将其转换为TreeView。 任何帮助,将不胜感激。 提前谢谢。
答案 0 :(得分:2)
试试这个。
static void add(TreeItem<String> node, String path) {
String[] items = path.substring(1).split("/");
for (int i = 0; i < items.length; ++i) {
TreeItem<String> found = null;
for (TreeItem<String> child : node.getChildren())
if (child.getValue().equals(items[i])) {
found = child;
break;
}
if (found == null) {
found = new TreeItem<>(items[i]);
node.getChildren().add(found);
}
node = found;
}
}
static void print(TreeItem<String> node, String indent) {
System.out.printf("%sTreeItem: %s%n", indent, node.getValue());
for (TreeItem<String> child : node.getChildren())
print(child, indent + " ");
}
和
String[] paths = {
"/applications/HI",
"/applications/HI/Hey.txt",
"/applications/HI/Milk",
"/applications/HI/Milk/blah",
"/applications/HI/Milk/HEADIAJQ/text.txt",
"/applications/HI/Milk/HEADIAJQ/thisworks!!!!.txt",
"/applications/HI/MilkiM/TRASHINGTHINGS.txt",
"/applications/HI/emoji.txt",
"/applications/Test",
"/applications/Test/Tjinsg",
};
TreeItem<String> root = new TreeItem<>("root");
for (String path : paths)
add(root, path);
print(root, "");
结果:
TreeItem: root
TreeItem: applications
TreeItem: HI
TreeItem: Hey.txt
TreeItem: Milk
TreeItem: blah
TreeItem: HEADIAJQ
TreeItem: text.txt
TreeItem: thisworks!!!!.txt
TreeItem: MilkiM
TreeItem: TRASHINGTHINGS.txt
TreeItem: emoji.txt
TreeItem: Test
TreeItem: Tjinsg
答案 1 :(得分:0)
以下是您需要将
folders
与files
分隔开来的课程:
import java.io.File;
import javafx.scene.control.TreeItem;
import javafx.scene.image.ImageView;
public class SystemFileItem extends TreeItem<String> {
/** Stores the full path to the file or directory */
private String fullPath;
private boolean isDirectory;
/**
* Constructor
*
* @param path
*/
public SystemFileItem(String path) {
super(path);
this.fullPath = path;
// test if this is a directory and set the icon
if (new File(fullPath).isDirectory()) {
isDirectory = true;
//setGraphic(new ImageView(SystemFilesTree.folderImage));
} // if you want different icons for different file types this is
// where you'd do it
else {
isDirectory = false;
//setGraphic(new ImageView(SystemFilesTree.fileImage));
}
// set the value
if (!fullPath.endsWith(File.separator)) {
// set the value (which is what is displayed in the tree)
String value = path;
int indexOf = value.lastIndexOf(File.separator);
if (indexOf > 0) {
this.setValue(value.substring(indexOf + 1));
} else {
this.setValue(value);
}
}
}
public String getFullPath() {
return fullPath;
}
public boolean isDirectory() {
return isDirectory;
}
}
另一个是相当多的代码逻辑(构建一个将文件添加到根等的方法):
SystemFileItem root = new SystemFileItem("Root File");
TreeView<String> treeView= new TreeView<String>(root);
libraryTreeView.setEditable(true);
SystemFileItem treeNode = new SystemFileItem("file path");
......