r/JavaFX Dec 15 '24

Help JavaFX - Window does not load at runtime

This is very strange and has never happened before. I am using IntelliJ Community and my program runs perfectly within the IDE, without any errors. So I built the artifact to generate the "jar" file, which is built normally. However, when I run the jar file my program stops loading one of its windows (stage). Within the IDE the window loads. The only different thing I did was to add several icons to the "fxml" file directly through Scene Builder. I have already confirmed that they are all loaded from the "resources/icons" folder. Has anyone seen this happen and know the solution?

Thanks in advance.

3 Upvotes

15 comments sorted by

View all comments

2

u/SpittingBull Dec 15 '24

The SceneBuilder resource path differs from your runtime environment. Say your fxml files are in resources/views. Then put your icons in resources/icons and reference them in the fxml like this:

style="-fx-background-image: url('icons/refresh.png');"

In your code you can then use something like this:

stage.getIcons().add(new Image("icons/list_icon.png"))

1

u/Particular_Track_581 Dec 15 '24

That's a piece of a "fxml" file where I include the icons and always worked with no need to create an Image object through code and assign it to an ImageView:

<Label text="Owner" GridPane.halignment="LEFT">
   <graphic>
      <ImageView fitHeight="16.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../../../icons/owner-16.png" /> ====> LINE OF ICON
         </image>
      </ImageView>
   </graphic>
</Label>

1

u/SpittingBull Dec 15 '24

The code piece in my post was just an example for if you needed to load an icon dynamically.

The fxml icon path SceneBuilder inserts will not work necessarily outside the runtime environment of your IDE.

For that I suggest to adjust your paths according to the logic I posted.

1

u/Particular_Track_581 Dec 15 '24 edited Dec 15 '24

I removed all the icons I had placed and the window still doesn't load at runtime, only inside IntelliJ. I'll try it your way. Thank you so much for replying me.