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.