如何在FXML中为TreeItem设置图形? [英] How can I set a graphic for a TreeItem in FXML?
问题描述
我正在尝试在FXML中配置TreeView的根节点:
I'm trying to configure the root node of my TreeView in FXML:
<TreeView fx:id="treeView" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<TreeItem expanded="false" value="Root" fx:id="rootItem" graphic="/icons/icon_folder.png" />
</TreeView>
有一个名为 graphic
的属性需要一个节点
,而不是我上面提供的 String
。
There is a property called graphic
which takes a Node
, and not a String
as I have supplied above.
如何在 FXML
中为其提供一个节点,该节点将代表存储在我的资源中的图像/ icons / icon_folder.png?
How do I give it a node in FXML
that would represent the image stored in my resource at icons/icon_folder.png?
在Java中,我可以执行以下操作:
In Java, I can do the following:
final Node iconFolder = new ImageView(
new Image(TreeArchiveSuite.class.getResourceAsStream("/icons/icon_folder.png"))
);
然后我可以在根节点上设置它:
And then I can just set it on the root node:
rootItem.setGraphic(iconFolder);
但是,我希望能够仅从FXML执行此操作。
However, I would like to be able to do this from FXML only.
推荐答案
找到 图形$ c $的按钮使用-fxml-only-how-to-an-image-to-a-button>类似问题 c>使用FXML进行 Button
控制的属性,我能够通过以下FXML标记实现我的目标:
After finding this similar question about setting the graphic
property for a Button
control with FXML, I was able to accomplish my goal with the following FXML markup:
<TreeItem expanded="false" value="Root" fx:id="rootItem">
<graphic>
<ImageView>
<image>
<Image url="icons/icon_folder.png" />
</image>
</ImageView>
</graphic>
</TreeItem>
这篇关于如何在FXML中为TreeItem设置图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!