5

I've been working on a big Java Desktop Application using the JavaFX SDK 11.0.2. In my application, I have many different Stage(s) for different set of functions. Now, I want to set icons on each and every window that opens in my application. While I know how to set an icon on a Stage, I'd like to know if there is a way that we can set up a single icon for all the windows in my application from a single point in code.

  • Is there any global icon setting or do I have to go and set icon on each and every stage in my application code ?
  • If I have to set the icon for every Stage, then is it fine to define a single Image object for the icon and pass it in every call to Stage.getIcons().add(image); or should I just initialize an Image object each time I show a Stage ?
Fuzail
  • 372
  • 5
  • 12
  • 2
    just a comment to your second part of the question: javadoc might be your friend :) – kleopatra Feb 17 '21 at 11:07
  • 1
    You could also declare a static final in your Main class, declare a getter, and call this getter everywhere, which is what I'm doing. – Remzi Cavdar Nov 02 '22 at 17:48

2 Answers2

4

As far as I know, there is not convenient property or method you can set so that all stages have the same icons automatically. That said...

In JavaFX 9 they added the Window#getWindows() method. There was a similar method in JavaFX 8 but it was private API. That method returns an observable list containing every window that is showing in the application. In other words, windows are added and removed from this list as they're shown and hidden, respectively. You can make use of this. For example:

public static void installIcons(Image... icons) {
  Window.getWindows().addListener((ListChangeListener<Window>) c -> {
    while (c.next()) {
      for (Window window : c.getAddedSubList()) {
        if (window instanceof Stage) {
          ((Stage) window).getIcons().setAll(icons);
        }
      }
    }
  });
}

This will set the icons of the stage to your icons every time it's shown. Unfortunately it will also set icons that don't need to be set, but that shouldn't have any noticeable effect on your application. Though I suppose you could find a way to ensure you only call setAll(icons) once per stage if you really need to.

Note that if all your extra windows are shown via Dialog then you don't need the above. If you set the owner of a Dialog it will automatically grab icons from the owner (if the owner is a Stage). I'm not sure if this is guaranteed behavior or not. Also note that dialogs use a stage under-the-hood, at least on desktop, so the above code will affect dialogs as well.

And while this is implied in the above example, the answer to your second question is: Yes, you can share the same Image instance with every stage.

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • So far I think this is the only closest solution to my problem. Thank you so much for the explanation. – Fuzail Feb 18 '21 at 06:57
0

I haven't tried this but you could have a property and use Stage onShowing event to define the specific Icon.

sproketboy
  • 8,967
  • 18
  • 65
  • 95