2015-02-23 28 views
0

我是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 :)

+0

什麼是tab41?我不確定這是否是添加treview的正確方法,直到您告訴我tab41是什麼類型的對象。 – 2015-02-23 07:33:31

+0

tab41是在splitPane中添加的選項卡對象。 :) – DeepSidhu1313 2015-02-23 07:39:49

+0

也許tab41.getChildren().add(FilesTree1.treeview) – 2015-02-23 07:45:29

回答

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()); 

       } 
      } 
     }); 
    } 
} 
} 

編輯:

提高了答案:)

我學習和試驗的代碼。沒有難過的感覺(所以不需要冷靜下來,如果你不鼓勵,可以學習如何學習,而不是幫助他人改進和學習,最終他/她將會得到它)

+0

如果您從任何地方複製代碼:1)將其歸因於源2)正確完整地複製BTW,如果它們是隨機格式的,則不完整的片段尤其難以閱讀(請閱讀格式幫助,點擊問號可用的fi)而且,與任何UI框架相同,您很少擴展TreeView - 而是擴展treeItem。還要注意,覆蓋isLeaf(沒有在你的代碼片段中顯示,但在java文檔示例中)並不是真正的選擇,因爲它違反了'isLeaf == leafProperty.get'不變量(請參閱https://javafx-jira.kenai。 COM /瀏覽/ RT-39762) – kleopatra 2015-02-23 12:50:56