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

Tree changes do net reset preferred size of window when using smartInvalidate

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Version:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 Pro, 64 bit
      OpenJDK 11.0.1

      A DESCRIPTION OF THE PROBLEM :
      When a component is added to or removed from a JFrame (or JDialog), and java.awt.smartinvalidate is set to true, invalidation stops at the root pane, because JRootPane.isValidateRoot() returns true. Thus, the JFrame is not invalidated in this case.

      This is an issue if the JFrame has a cached preferred size, since the size depends on the content and therefore
      should be recalculated.

      To fix this, there are several possibilities.

      1) Don't cache preferred size.
      2) Return false from JRootPane.isValidateRoot(), so the Window gets invalidated as well. To make this work, JViewport.validateView() would need to be changed as well, because it searches for the nearest ancestor that isValidateRoot() and casts it to JComponent, which works with JRootPane but doesn't work with JFrame or JDialog.
      3) Always invalidate parent if instanceof Window, even if this instance isValidateRoot() already. This is sort of a hack that fixes the issue without having to change JViewport or the caching of preferred size.
         

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the provided source code twice, once with VM arguments "-Djava.awt.smartInvalidate=true" and once without. In both cases, click on the "Remove me" button of the JFrame that pops up and observe.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Regardless of VM arguments, the button should be removed and the JFrame should shrink accordingly, since JFrame.pack() is called after removing the button.
      ACTUAL -
      With -Djava.awt.smartInvalidate=true, the JFrame does not shrink after removing the button, whereas without VM arguments everything works as expected.

      ---------- BEGIN SOURCE ----------
      package it.prodata.test.swing;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.WindowConstants;

      public class Invalidate {

      public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      JButton b = new JButton("Remove me");
      b.addActionListener(e -> {
      f.getPreferredSize(); // Causes prefSize to be cached
      f.remove(b); // Doesn't reset prefSize if java.awt.smartInvalidate=true
      f.pack();
      });
      f.add(b);
      f.pack();
      f.setVisible(true);
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: