我在.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;
请给我任何参考或提示。
您在使用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());
}
}
});
您的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;
所以你不需要为你的树创建一个根。你完了。