如何在FXML中为TreeItem设置图形? [英] How can I set a graphic for a TreeItem in FXML?

查看:799
本文介绍了如何在FXML中为TreeItem设置图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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进行 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆