2013-12-14 36 views
0

我在.fxml文件中創建了TreeView,然後我試圖顯示根節點。但沒有顯示。在TreeView中沒有顯示根節點(TreeItem)

這是我的代碼。

<TabPane prefHeight="289.0" prefWidth="246.0" styleClass="tab-pane" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
        <stylesheets> 
        <URL value="@main.css" /> 
        </stylesheets> 
        <tabs> 
        <Tab text="TestBed Explorer"> 
         <content> 
         <AnchorPane id="Content" fx:id="soariteAnchorScollparent" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> 
          <children> 
          <ScrollPane fx:id="soariteTreeScrollPane" prefHeight="259.0" prefWidth="246.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
           <content> 
           <AnchorPane id="Content" fx:id="soariteTreeAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="249.0" prefWidth="73.0"> 
            <children> 
            <TreeView fx:id="soariteTree" prefHeight="245.0" prefWidth="79.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="167.0" AnchorPane.topAnchor="0.0"> 
             <TreeItem expanded="true" value="categories" fx:id="rootTreeItem" /> 
            </TreeView> 
            </children> 
           </AnchorPane> 
           </content> 
          </ScrollPane> 
          </children> 
         </AnchorPane> 
         </content> 
        </Tab> 
        </tabs> 
       </TabPane>  

我也給這個主要類的參考喜歡。

public class Mainextends Application { 
@FXML 
public TreeView<String> soariteTree; 
@FXML 
public TreeItem<String> rootTreeItem; 

請給我任何參考或提示。

回答

5

你已經做了一個小錯誤與FXML,

你可以看到你寫的AnchorPane.rightAnchor="167.0"這是使你的樹視圖中消失(和相同的小失誤錨窗格和樹視圖的寬度)。

替換滾動窗格的你,

<ScrollPane fx:id="soariteTreeScrollPane" prefHeight="259.0" prefWidth="246.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
           <content> 
           <AnchorPane id="Content" fx:id="soariteTreeAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="-1.0" prefWidth="-1.0"> 
            <children> 
            <TreeView fx:id="soariteTree" prefHeight="-1.0" prefWidth="-1.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
             <TreeItem expanded="true" value="categories" fx:id="rootTreeItem" /> 
            </TreeView> 
            </children> 
           </AnchorPane> 
           </content> 
          </ScrollPane> 

更新: - 處理鼠標的事件

soariteTree.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      if (event.getButton().equals(MouseButton.SECONDARY)) { 
       System.out.println(">> " + event.getEventType()); 
      } 
     } 
    }); 
+0

你可以再做一個幫助....如何在此上添加鼠標右鍵點擊樹? –

+0

是你的問題解決了,然後把這個標記爲正確的答案,並且肯定會嘗試點擊右鍵並在以後發帖 –

+0

是的..你解決了我的問題..至少20小時需要獎勵賞金... –

2

你FXML會,

<TreeView fx:id="categoryTreeView" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minWidth="200.0" prefHeight="-1.0" prefWidth="200.0" showRoot="true" styleClass="master-tree" VBox.vgrow="ALWAYS"> 
      <TreeItem expanded="true" value="categories" fx:id="categoryTreeItem" /> 
</TreeView> 

和你的控制器會,

public class MyClass extends Application { 
@FXML private TreeItem<ItemMaster> categoryTreeItem; 
@FXML private TreeView<ItemMaster> categoryTreeView; 

所以你不需要創建一個根的樹。你完成了。

+0

更新我的問題....看到 –

+0

刪除線,myTree.setRoot(royalRoot );你不需要重新設置root。並在您的fxml中寫入showRoot =「true」 –

+0

我已經刪除了這兩行代碼 –