我是Java和JavaFX的新手,我正在使用使用SplitPane的GUI應用程序。在一個窗格中,我試圖顯示filetree,並在其他我已成功添加TabPane。 使用filetree用戶將以選項卡的形式打開文件。如何在JavaFX中生成FileTree?
我正在使用此代碼來生成文件樹。
public class FilesTree1 implements Runnable {
static CustomTree filetree = new CustomTree();
public static TreeView treeview;
//File f = new File("workspace");
int timeout = 0;
public FilesTree1() {
// this.getTree();
}
private TreeView buildFileSystemBrowser() {
TreeItem root = createNode(new File("workspace"));
return new TreeView(root);
}
private TreeItem createNode(final File f) {
TreeItem treeItem = null;
if (f.isDirectory() && f.listFiles() == null) {
return treeItem = new TreeItem("Empty");
}
File[] tempfilelist = f.listFiles();
for (int i = 0; i < tempfilelist.length; i++) {
if (tempfilelist[i].isDirectory()) {
System.out.println("- " + tempfilelist[i]);
treeItem = new TreeItem(createNode(tempfilelist[i]));
} else {
treeItem = new TreeItem((tempfilelist[i].getAbsolutePath()));
System.out.println("- " + tempfilelist[i]);
}
}
return treeItem;
}
@Override
public void run() {
while (true) {
try {
// filetree.setSimpleRoot(f.getName());
treeview = this.buildFileSystemBrowser();
treeview.getRoot().setExpanded(true);
Thread.sleep(100000);
} catch (InterruptedException ex) {
}
}
}
}
在主類中我調用此函數在窗格中添加樹。
static Tab tab41 = new Tab("Files");
public void synchroniseUi() {
Platform.runLater(new Runnable() {
@Override
public void run() {
tab41.setContent(FilesTree1.treeview);
}
});
}
但問題是樹沒有出現在窗格中。
編輯:tab41是一個選項卡SplitPane :)
什麼是tab41?我不確定這是否是添加treview的正確方法,直到您告訴我tab41是什麼類型的對象。 – 2015-02-23 07:33:31
tab41是在splitPane中添加的選項卡對象。 :) – DeepSidhu1313 2015-02-23 07:39:49
也許tab41.getChildren().add(FilesTree1.treeview) – 2015-02-23 07:45:29