diff --git a/apps/toys/Hello/src/main/java/hello/HelloPopup.java b/apps/toys/Hello/src/main/java/hello/HelloPopup.java --- a/apps/toys/Hello/src/main/java/hello/HelloPopup.java +++ b/apps/toys/Hello/src/main/java/hello/HelloPopup.java @@ -128,11 +128,10 @@ } nextPopup.show(popupParent, popupX, popupY); - - Iterator windows = Window.impl_getWindows(); - while (windows.hasNext()) { - System.out.println("W: " + windows.next().getClass().getName()); - } + + Window.getWindows().stream().forEach(window -> { + System.out.println("W: " + window.getClass().getName()); + }); } } diff --git a/modules/graphics/src/main/java/com/sun/javafx/tk/Toolkit.java b/modules/graphics/src/main/java/com/sun/javafx/tk/Toolkit.java --- a/modules/graphics/src/main/java/com/sun/javafx/tk/Toolkit.java +++ b/modules/graphics/src/main/java/com/sun/javafx/tk/Toolkit.java @@ -855,14 +855,12 @@ */ public void pauseScenes() { pauseScenesLatch = new CountDownLatch(1); - Iterator i = Window.impl_getWindows(); - while (i.hasNext()) { - final Window w = i.next(); - final Scene scene = w.getScene(); + Window.getWindows().stream().forEach(window -> { + final Scene scene = window.getScene(); if (scene != null) { this.removeSceneTkPulseListener(scene.impl_getScenePulseListener()); } - } + }); this.getMasterTimer().pause(); SceneHelper.setPaused(true); } @@ -874,14 +872,12 @@ public void resumeScenes() { SceneHelper.setPaused(false); this.getMasterTimer().resume(); - Iterator i = Window.impl_getWindows(); - while (i.hasNext()) { - final Window w = i.next(); - final Scene scene = w.getScene(); + Window.getWindows().stream().forEach(window -> { + final Scene scene = window.getScene(); if (scene != null) { this.addSceneTkPulseListener(scene.impl_getScenePulseListener()); } - } + }); pauseScenesLatch.countDown(); pauseScenesLatch = null; } diff --git a/modules/graphics/src/main/java/javafx/stage/Window.java b/modules/graphics/src/main/java/javafx/stage/Window.java --- a/modules/graphics/src/main/java/javafx/stage/Window.java +++ b/modules/graphics/src/main/java/javafx/stage/Window.java @@ -28,8 +28,10 @@ import java.security.AllPermission; import java.security.AccessControlContext; import java.security.AccessController; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import javafx.beans.property.DoubleProperty; import javafx.beans.property.DoublePropertyBase; @@ -134,20 +136,25 @@ } /** - * Return all Windows + * Returns a list containing a reference to the currently existing JavaFX windows. The list, whilst mutable, is + * simply a reflection of the state at that moment in time - modifying the list will have no impact at all. * - * @return Iterator of all Windows - * @treatAsPrivate implementation detail - * @deprecated This is an internal API that is not intended for use and will be removed in the next version + *

It is strong encouraged that developers do not retain a reference to this list outside of their immediate + * use case (i.e. within the relevant method), as doing so can lead to memory leaks.

+ * + * @return A list containing all windows currently showing. + * @since 9 */ - @Deprecated - public static Iterator impl_getWindows() { + public static List getWindows() { final SecurityManager securityManager = System.getSecurityManager(); if (securityManager != null) { securityManager.checkPermission(new AllPermission()); } - return (Iterator) windowQueue.iterator(); + Iterator it = (Iterator) windowQueue.iterator(); + List windows = new ArrayList<>(); + it.forEachRemaining(windows::add); + return windows; } final AccessControlContext acc = AccessController.getContext(); diff --git a/modules/jmx/src/main/java/com/oracle/javafx/jmx/SGMXBeanImpl.java b/modules/jmx/src/main/java/com/oracle/javafx/jmx/SGMXBeanImpl.java --- a/modules/jmx/src/main/java/com/oracle/javafx/jmx/SGMXBeanImpl.java +++ b/modules/jmx/src/main/java/com/oracle/javafx/jmx/SGMXBeanImpl.java @@ -362,12 +362,10 @@ private void importWindows() { int windowCount = 0; - final Iterator it = Window.impl_getWindows(); + final List windows = Window.getWindows(); jwindows = JSONDocument.createArray(); - while (it.hasNext()) { - final Window window = it.next(); - + for (Window window : windows) { windowMap.put(windowCount, window); final JSONDocument jwindow = JSONDocument.createObject(); diff --git a/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_sceneGraph_Test.java b/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_sceneGraph_Test.java --- a/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_sceneGraph_Test.java +++ b/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_sceneGraph_Test.java @@ -43,6 +43,7 @@ import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Set; public class SGMXBean_sceneGraph_Test { @@ -74,12 +75,10 @@ stage2.setScene(scene2); stage2.show(); - final Iterator it = Window.impl_getWindows(); + final List windows = Window.getWindows(); nodesCount = 0; - while (it.hasNext()) { - final Window w = it.next(); + for (Window w : windows) { nodesCount += countNodes(w.getScene().getRoot()); - } } diff --git a/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_windows_Test.java b/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_windows_Test.java --- a/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_windows_Test.java +++ b/modules/jmx/src/test/java/com/oracle/javafx/jmx/SGMXBean_windows_Test.java @@ -42,6 +42,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import java.util.List; public class SGMXBean_windows_Test { @@ -66,12 +67,8 @@ stage2.setScene(scene2); stage2.show(); - final Iterator it = Window.impl_getWindows(); - windowsCount = 0; - while (it.hasNext()) { - windowsCount++; - it.next(); - } + final List windows = Window.getWindows(); + windowsCount = windows.size(); } private static JSONDocument getJSONDocument(String source) {