3

I have a utility class that reloads CSS. To get all stages on the scene graph I am using

com.sun.javafx.stage.StageHelper#getStages()

In Java 9 this is no longer accessible without specifying --add-exports during compile.

--add-exports=javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED

I am looking for an alternative to StageHelper in getting all stages on the scene graph.

There is a public API equivalent to StageHelper.getStages() in Window.getWindows().

However there is a slight difference between these two:

StageHelper.getStages(): Returns a ObservableList containing Stages created at this point

Window.getWindows(): Returns a list containing a reference to the currently showing JavaFX windows.

What I get from this is that using Window instead of StageHelper will not get me the Stages that are invisible/hidden.

In Java 8 Window.impl_getWindows() returned all the windows, and it says nothing of visibility.

A list of all the currently existing windows

This changed with Java 9 Window.getWindows()

A list of all the currently showing windows

DJViking
  • 832
  • 1
  • 12
  • 29
  • hmm ... might be an option to listen to the list of windows and keep your own containing all. Or maybe reload the css whenever a window gets visible .. not nice, none of it .. – kleopatra Dec 11 '17 at 11:37
  • yet another hmm: a) in fx9 there is no StageHelper.getStages() b) in fx8 the windows/stages stored seem to be visible only also - they seem to be removed from their corresponding queue/list on visibility change ... confused ;) – kleopatra Dec 11 '17 at 12:06
  • Use new API Window.getWindows() – Valery-Sh May 07 '18 at 08:30
  • Valery: The new API Window.getWindows() works only with Java 9. In Java 8 the method name is different. – DJViking May 07 '18 at 08:32

1 Answers1

2

Internal API and deprecated methods should be avoided. However with Java 8 there is no choice to use these if you need to get all the stages on the scene graph.

Solution when you need support for Java 8:

com.sun.javafx.stage.StageHelper.getStages()

This can work with Java 9 by specifying --add-exports as a command line argument to java.

--add-exports=javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED


Avoid using Window.impl_getWindows() as it will not run on Java 9.
The method name and returned value is different

Java 8:

Iterator<Window> itr = Window.impl_getWindows();

Java 9:

List<Window> windows = Window.getWindows();


Solution for Java 9:
Use Window.getWindows() to acquire all the stages.

The method description for Window.getWindows():

Returns a list containing a reference to the currently showing JavaFX windows.

One could think this will only return a list of windows that are visible. I have tested this and it will list all windows, even those that are minimized (i.e. not visible). It is not the same as the description says about showing. It means the showing property is set true after calling stage.show().

DJViking
  • 832
  • 1
  • 12
  • 29