diff --git a/modules/javafx.swing/src/main/java/javafx/embed/swing/JFXPanel.java b/modules/javafx.swing/src/main/java/javafx/embed/swing/JFXPanel.java index edfe0464ce4..21f6ae430ec 100644 --- a/modules/javafx.swing/src/main/java/javafx/embed/swing/JFXPanel.java +++ b/modules/javafx.swing/src/main/java/javafx/embed/swing/JFXPanel.java @@ -299,12 +299,14 @@ public void setScene(final Scene newScene) { (PrivilegedAction) java.awt.Toolkit .getDefaultToolkit()::getSystemEventQueue); SecondaryLoop secondaryLoop = eventQueue.createSecondaryLoop(); - if (secondaryLoop.enter()) { - Platform.runLater(() -> { + Platform.runLater(() -> { + try { setSceneImpl(newScene); - }); - secondaryLoop.exit(); - } + } finally { + secondaryLoop.exit(); + } + }); + secondaryLoop.enter(); } } diff --git a/tests/system/src/test/java/test/javafx/embed/swing/JFXPanelTest.java b/tests/system/src/test/java/test/javafx/embed/swing/JFXPanelTest.java index 217295207a1..95efad1bfd9 100644 --- a/tests/system/src/test/java/test/javafx/embed/swing/JFXPanelTest.java +++ b/tests/system/src/test/java/test/javafx/embed/swing/JFXPanelTest.java @@ -28,6 +28,7 @@ import org.junit.Assume; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.After; import org.junit.AfterClass; import org.junit.Test; import junit.framework.AssertionFailedError; @@ -54,6 +55,8 @@ public class JFXPanelTest { // Used to launch the application before running any test private static final CountDownLatch launchLatch = new CountDownLatch(1); + JFrame jframe; + // Application class. An instance is created and initialized before running // the first test, and it lives through the execution of all tests. public static class MyApp extends Application { @@ -84,6 +87,13 @@ public static void doTeardownOnce() { Platform.exit(); } + @After + public void doCleanup() { + if (jframe != null) { + SwingUtilities.invokeLater(() -> jframe.dispose()); + } + } + static class TestFXPanel extends JFXPanel { protected void processMouseEventPublic(MouseEvent e) { processMouseEvent(e); @@ -103,7 +113,7 @@ public void testNoDoubleClickOnFirstClick() throws Exception { dummyFXPanel.setPreferredSize(new Dimension(100, 100)); TestFXPanel fxPnl = new TestFXPanel(); fxPnl.setPreferredSize(new Dimension(100, 100)); - JFrame jframe = new JFrame(); + jframe = new JFrame(); JPanel jpanel = new JPanel(); jpanel.add(dummyFXPanel); jpanel.add(fxPnl); @@ -155,5 +165,58 @@ public void testClickOnEmptyJFXPanel() throws Exception { Assert.assertTrue(firstPressedEventLatch.await(5000, TimeUnit.MILLISECONDS)); } -} + @Test + public void setSceneOnFXThread() throws Exception { + + CountDownLatch completionLatch = new CountDownLatch(1); + + SwingUtilities.invokeLater(() -> { + JFXPanel fxPanel = new JFXPanel(); + fxPanel.setPreferredSize(new Dimension(100, 100)); + jframe = new JFrame(); + JPanel jpanel = new JPanel(); + jpanel.add(fxPanel); + jframe.add(jpanel); + jframe.pack(); + jframe.setVisible(true); + + Platform.runLater(() -> { + Scene scene = new Scene(new Group()); + fxPanel.setScene(scene); + completionLatch.countDown(); + }); + }); + + Assert.assertTrue("Timeout waiting for setScene to complete", + completionLatch.await(5000, TimeUnit.MILLISECONDS)); + } + + @Test + public void setSceneOnSwingThread() throws Exception { + + CountDownLatch completionLatch = new CountDownLatch(1); + + SwingUtilities.invokeLater(() -> { + JFXPanel fxPanel = new JFXPanel(); + fxPanel.setPreferredSize(new Dimension(100, 100)); + jframe = new JFrame(); + JPanel jpanel = new JPanel(); + jpanel.add(fxPanel); + jframe.add(jpanel); + jframe.pack(); + jframe.setVisible(true); + + Platform.runLater(() -> { + Scene scene = new Scene(new Group()); + SwingUtilities.invokeLater(() -> { + fxPanel.setScene(scene); + completionLatch.countDown(); + }); + }); + }); + + Assert.assertTrue("Timeout waiting for setScene to complete", + completionLatch.await(5000, TimeUnit.MILLISECONDS)); + } +}