Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8334593

Adding, removing and then adding a JFXPanel again leads to NullPointerException

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10
      Java 21 or later
      JavaFX 21 or later


      A DESCRIPTION OF THE PROBLEM :
      Adding, then removing, and then adding a JFXPanel to the same component in the Swing scene graph leads to a NullPointerException in GlassScene because the sceneState is null. The use case and where this bug was noticed is to show and hide tabs in a JTabbedPane, but the bug appears to be reproducible for any Swing component that can take children.

      REGRESSION : Last worked in version 20

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create an instance of a JFXPanel.
      2. Add the JFXPanel to a Swing component X as a child.
      3. Remove the JFXPanel instance from X.
      4. Add the JFXPanel instance again to Swing component X as a child.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The JFXPanel is visible to the user of the application and no Exceptions are thrown.
      ACTUAL -
      The JFXPanel is visible but the following Exception is thrown:

      Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "com.sun.javafx.tk.quantum.SceneState.update()" because "this.sceneState" is null
      at javafx.graphics/com.sun.javafx.tk.quantum.GlassScene.updateSceneState(GlassScene.java:285)
      at javafx.graphics/com.sun.javafx.tk.quantum.EmbeddedScene.lambda$setPixelScaleFactors$1(EmbeddedScene.java:159)
      at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithRenderLock(QuantumToolkit.java:448)
      at javafx.graphics/com.sun.javafx.tk.quantum.EmbeddedScene.lambda$setPixelScaleFactors$2(EmbeddedScene.java:158)
      at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
      at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
      at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
      at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
      at java.base/java.lang.Thread.run(Thread.java:1570)

      ---------- BEGIN SOURCE ----------
      import javafx.application.Platform;
      import javafx.embed.swing.JFXPanel;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;

      import javax.swing.*;

      public class FxTest {

          private static void initAndShowGUI() {
              JFrame frame = new JFrame("FX");
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              final JFXPanel fxPanel = new JFXPanel();
              // fxPanel added to frame for the first time
              frame.add(fxPanel);
              frame.setVisible(true);

              Platform.runLater(() -> {
                  initFX(fxPanel);
                  SwingUtilities.invokeLater(() -> {
                      // fxPanel removed from frame
                      frame.remove(fxPanel);
                      // fxPanel added to frame again
                      frame.add(fxPanel); // <-- leads to NullPointerException
                  });
              });
          }

          private static void initFX(JFXPanel fxPanel) {
              Scene scene = new Scene(new Button("Testbutton"));
              fxPanel.setScene(scene);
          }

          public static void main(String[] args) {
              SwingUtilities.invokeLater(() -> initAndShowGUI());
          }

      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: