我是Java和JavaFX的新手,我正在开发一个使用SplitPane的GUI应用程序。在一个窗格中,我正在尝试显示filetree,而在其他窗格中,我已成功添加了TabPane。 使用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中的一个标签:)
答案 0 :(得分:0)
好的,所以我终于解决了问题,这是我最新的工作代码。希望它可能对其他人有用。
public class FilesTree implements Runnable {
static CustomTree filetree = new CustomTree();
;
public static TreeView<File> tv = new TreeView();
public static Image folderCollapseImage = new Image(ClassLoader.getSystemResourceAsStream("ui/folder.png"));
public static Image folderExpandImage = new Image(ClassLoader.getSystemResourceAsStream("ui/folder-open.png"));
public static Image fileImage = new Image(ClassLoader.getSystemResourceAsStream("ui/file.png"));
SQLiteJDBC treedb = new SQLiteJDBC();
String sql;
ResultSet rs;
int totalFolder = 0;
int totalFile = 0;
File[] filelist;
File f = new File("workspace");
int timeout = 0;
public FilesTree() {
// this.getTree();
}
private TreeView buildFileSystemBrowser() {
TreeItem<File> root = createNode(new File("workspace"));
return new TreeView<File>(root);
}
// This method creates a TreeItem to represent the given File. It does this
// by overriding the TreeItem.getChildren() and TreeItem.isLeaf() methods
// anonymously, but this could be better abstracted by creating a
// 'FileTreeItem' subclass of TreeItem. However, this is left as an exercise
// for the reader.
private TreeItem<File> createNode(final File f) {
return new TreeItem<File>(f) {
// We cache whether the File is a leaf or not. A File is a leaf if
// it is not a directory and does not have any files contained within
// it. We cache this as isLeaf() is called often, and doing the
// actual check on File is expensive.
private boolean isLeaf;
// We do the children and leaf testing only once, and then set these
// booleans to false so that we do not check again during this
// run. A more complete implementation may need to handle more
// dynamic file system situations (such as where a folder has files
// added after the TreeView is shown). Again, this is left as an
// exercise for the reader.
private boolean isFirstTimeChildren = true;
private boolean isFirstTimeLeaf = true;
@Override
public ObservableList<TreeItem<File>> getChildren() {
if (isFirstTimeChildren) {
isFirstTimeChildren = false;
// First getChildren() call, so we actually go off and
// determine the children of the File contained in this TreeItem.
super.setExpanded(true);
this.setExpanded(true);
super.getChildren().setAll(buildChildren(this));
}
return super.getChildren();
}
@Override
public boolean isLeaf() {
if (isFirstTimeLeaf) {
isFirstTimeLeaf = false;
File f = (File) getValue();
isLeaf = f.isFile();
}
return isLeaf;
}
private ObservableList<TreeItem<File>> buildChildren(TreeItem<File> TreeItem) {
File f = TreeItem.getValue();
if (f != null && f.isDirectory()) {
// super.setGraphic(new ImageView(folderCollapseImage));
File[] files = f.listFiles();
TreeItem.setExpanded(true);
if (files != null) {
ObservableList<TreeItem<File>> children = FXCollections.observableArrayList();
for (int i = 0; i < files.length; i++) {
children.add(createNode(files[i]));
if (files[i].isDirectory()) {
children.get(i).setGraphic(new ImageView(folderCollapseImage));
} else {
children.get(i).setGraphic(new ImageView(fileImage));
}
children.get(i).addEventHandler(TreeItem.branchCollapsedEvent(), new EventHandler() {
@Override
public void handle(Event e) {
TreeItem<File> source = (TreeItem<File>) e.getSource();
File source2= source.getValue();
if (source2.isDirectory() && !source.isExpanded()) {
ImageView iv = (ImageView) source.getGraphic();
iv.setImage(folderCollapseImage);
}
}
});
children.get(i).addEventHandler(TreeItem.branchExpandedEvent(), new EventHandler() {
@Override
public void handle(Event e) {
TreeItem<File> source = (TreeItem<File>) e.getSource();
File source2= source.getValue();
if (source2.isDirectory() && source.isExpanded()) {
ImageView iv = (ImageView) source.getGraphic();
iv.setImage(folderExpandImage);
}
}
});
}
return children;
}
}
return FXCollections.emptyObservableList();
}
};
}
@Override
public void run() {
//while (true)
{
filetree.setSimpleRoot(f.getName());
tv = this.buildFileSystemBrowser();
tv.getRoot().setExpanded(true);
//tv.setSelectionModel(null);
tv.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getClickCount() % 2 == 0) {
MultipleSelectionModel msm = tv.getSelectionModel();
TreeItem<File> item = (TreeItem<File>) msm.getSelectedItem();
System.out.println("Selected Text : " + item.getValue());
// Create New Tab
} else {
MultipleSelectionModel msm = tv.getSelectionModel();
TreeItem<File> item = (TreeItem<File>) msm.getSelectedItem();
System.out.println("Selected Text : " + item.getValue().getAbsolutePath());
}
}
});
}
}
}
编辑:
改进答案:)
我正在学习和试验代码。没有强烈的感情(所以不需要贬低,如果你保持沮丧,怎么能学会?反过来帮助改善和学习,最终他/她会做对)