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

时间:2013-12-14 10:43:41

标签: 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>      

我也在主类中提到这个。

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

请给我任何参考或提示。

2 个答案:

答案 0 :(得分: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());
            }
        }
    });

答案 1 :(得分: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;

所以你不需要为你的树创建一个根。你完成了。