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

SwingNode does not resize when content size constraints are changed

XMLWordPrintable

    • b20
    • x86
    • other

      FULL PRODUCT VERSION :
      java version "1.8.0_77"
      Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
      Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      SwingNode does not update its internal cache of Swing pref/max/min height and widths when its JComponent content's corresponding size constraints are updated. As such, it isn't resized to honor those size constraints.

      The private implementation class SwingNodeContent does contain methods preferredSizeChanged(), maximumSizeChanged(), and minimumSizeChanged(), but these are only called once from JLightweightFrame.setContent() when the JComponent is first installed via SwingNode.setContent().

      JLightweightFrame does install a PropertyChangeListener for "preferredSize", "maximumSize", and "minimumSize" properties, but this only happens via a ContainerListener which is not added until after the content has already been added to the content pane. The component installed via SwingNode.setContent() never gets these property change listeners added.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - Create a SwingNode
      - Set a JComponent as the SwingNode's content
      - Add the SwingNode to a Scene/Stage and show it
      - Modify the JComponent's pref/max/min size


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      According to the documentation for the SwingNode.resize() method: "Applications should not invoke this method directly. If an application needs to directly set the size of the SwingNode, it should set the Swing component's minimum/preferred/maximum size constraints which will be propagated correspondingly to the SwingNode and it's parent will honor those settings during layout."

      ACTUAL -
      The SwingNode's size does NOT honor its Swing components minimum/preferred/maximum size constraints.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class SwingNodeResize extends Application
      {
          StackPane root;

          @Override
          public void start(Stage primaryStage)
          {
              SwingNode swingNode = new SwingNode();

              init(swingNode);

              root = new StackPane(new Rectangle(500, 500, Color.RED), swingNode);
              Scene scene = new Scene(root);

              primaryStage.setTitle("SwingNode Resize");
              primaryStage.setScene(scene);
              primaryStage.show();
          }

          private void init(final SwingNode node)
          {
              SwingUtilities.invokeLater(() ->
              {
                  JButton button = new JButton("Click me!");
                  button.addActionListener(event ->
                  {
                      Dimension buttonSize = button.getSize();
                      buttonSize.setSize(buttonSize.getWidth() * 2, buttonSize.getHeight() * 2);

                      button.setPreferredSize(buttonSize);
                      button.setMinimumSize(buttonSize);
                      button.setMaximumSize(buttonSize);
                      button.setSize(buttonSize);

                      System.out.println("Button size: " + button.getPreferredSize());
                      Platform.runLater(() -> System.out.println("SwingNode size: " + node.prefWidth(-1) + " " + node.prefHeight(-1)));
                  });

                  node.setContent(button);
              });
          }

          public static void main(String[] args)
          {
              launch(args);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      - Removing the JComponent from the SwingNode
      - Modify the JComponent's size constraints
      - Reinstall the JComponent as the SwingNode's content

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

              Created:
              Updated:
              Resolved: