根节点(TreeItem)未显示在TreeView中

根节点(TreeItem)未显示在TreeView中,treeview,javafx-2,Treeview,Javafx 2,我在.fxml文件中创建TreeView,然后尝试显示根节点。但是它没有显示出来 这是我的密码 <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.to

我在.fxml文件中创建TreeView,然后尝试显示根节点。但是它没有显示出来

这是我的密码

<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;
公共类main扩展应用程序{
@FXML
公共树;观赏树;
@FXML
公共树;根树;
请给我任何参考或提示。

您的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;
公共类MyClass扩展应用程序{
@FXML私有树项categoryTreeItem;
@FXML私有树视图类别树视图;

因此,您不需要为树创建根。您已经完成了。

您在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());
            }
        }
    });
soariteTree.addEventHandler(MouseEvent.MOUSE_按下,new EventHandler()){
@凌驾
公共无效句柄(MouseeEvent事件){
if(event.getButton().equals(MouseButton.SECONDARY)){
System.out.println(“>>”+event.getEventType());
}
}
});

删除行,myTree.setRoot(royalRoot);您不需要再次设置根。并在fxml中写入showRoot=“true”您现在是否遇到异常?并在fxml中写入showRoot=“true”。无任何异常…我添加showRoot=“true”…仍然未显示您尚未将fx:id指定给TreeItem,您必须在此根目录树中添加更多的树项。您的问题是否已解决,然后将其标记为正确答案。请确保稍后尝试右键单击并发布。