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

San*_*til 0 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.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>      
Run Code Online (Sandbox Code Playgroud)

我也在主要类中给出了这个参考.

public class Mainextends Application {
@FXML
public TreeView<String> soariteTree;
@FXML
public TreeItem<String> rootTreeItem;
Run Code Online (Sandbox Code Playgroud)

请给我任何参考或提示.

Shr*_*ave 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>
Run Code Online (Sandbox Code Playgroud)

更新: - 处理鼠标事件

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());
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)