GoXR3Plus GoXR3Plus - 28 days ago 18
Java Question

JavaFX How set ImageView from SceneBuilder

As i see in

SceneBuilder
you can set graphic for
ImageView
(though that image will not work if you export the project as .jar and you have the image into the jar).

1)I want to set images for some fxml buttons through
SceneBuilder
but i can't find anything about that.

2)Also it will be usefull if you provide info on how to do with
.fxml
what i do with
getClass().getResource("image.png");
,so i have not to do this on the java file manually.


Edit:


Both answers from Fabian and are John Vernee are correct each one for question 1 and 2 but i can choose only one.

The structure of resources folder:

enter image description here

Answer

In SceneBuilder drag the graphic node you want to use (in this case ImageView) from the Library to the Button in the Hierarchy view. Drop it there and you should see the graphic node displayed as a child of the Button.

This will result in a fxml like this

...
<Button mnemonicParsing="false" text="Button">
    <graphic>
        <ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
    </graphic>
</Button>
...

By using the gear button next to the image property of the ImageView you can specify a image url starting with @ by selecting Switch to document relative path. Input image.png for the url @image.png. Saving the fxml to the correct location relative to the image will display the image in SceneBuilder.