How can I set a graphic for a TreeItem in FXML?

断了今生、忘了曾经 提交于 2020-01-05 19:00:18

问题


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>

There is a property called graphic which takes a Node, and not a String as I have supplied above.

How do I give it a node in FXML that would represent the image stored in my resource at icons/icon_folder.png?

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);

However, I would like to be able to do this from FXML only.


回答1:


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>


来源:https://stackoverflow.com/questions/21889697/how-can-i-set-a-graphic-for-a-treeitem-in-fxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!