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

JScrollBar.getMinimumSize() breaks the contract of JComponent.setMinimumSize()

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 23
    • client-libs
    • None
    • behavioral
    • low
    • Addition of method overrides to explain variation from super class javadoc, with no resulting behavioral change
    • Java API
    • SE

      Summary

      JComponent's setMinimumSize and setMaximumSize are overridden in JScrollBar to specify that JScrollBar's getMinimumSize and getMaximumSize returns a size derived from the preferred size in one axis and fixed value in the other axis.

      Problem

      The javadoc for JComponent.setMinimumSize(java.awt.Dimension) states:

      "Sets the minimum size of this component to a constant value. Subsequent calls to getMinimumSize will always return this value..."

      However, JScrollBar overrides getMinimumSize() and breaks this contract.

      The returned Dimension is composed of the preferred width and a fixed minimum height, or vice versa depending on the orientation of the scrollbar.

      Similarly, JScrollBar.getMaximumSize() uses the preferred width or height and a fixed maximum for the other dimension.

      There's risk that changing this might cause the layout of applications to change in a way that breaks the applications. That change should have been made a long time ago.

      Solution

      JScrollBar's setMinimumSize() and setMaximumSize() are overridden to clarify the spec

      Specification

      javax.swing.JComponent.setMaximumSize

           /**
            * Sets the maximum size of this component to a constant
      -     * value.  Subsequent calls to <code>getMaximumSize</code> will always
      +     * value.  Subsequent calls to {@code getMaximumSize} will always
            * return this value; the component's UI will not be asked
      -     * to compute it.  Setting the maximum size to <code>null</code>
      +     * to compute it. Setting the maximum size to {@code null}
            * restores the default behavior.
      +     * <p>
      +     * Subclasses may choose to override this by returning their own maximum size
      +     * in the {@code getMaximumSize} method.
            *
            * @param maximumSize a <code>Dimension</code> containing the
            *          desired maximum allowable size
            * @see #getMaximumSize
            */
           public void setMaximumSize(Dimension maximumSize) {

      javax.swing.JComponent.setMinimumSize

           /**
            * Sets the minimum size of this component to a constant
      -     * value.  Subsequent calls to <code>getMinimumSize</code> will always
      +     * value.  Subsequent calls to {@code getMinimumSize} will always
            * return this value; the component's UI will not be asked
      -     * to compute it.  Setting the minimum size to <code>null</code>
      +     * to compute it. Setting the minimum size to {@code null}
            * restores the default behavior.
      +     * <p>
      +     * Subclasses may choose to override this by returning their own minimum size
      +     * in the {@code getMinimumSize} method.
            *
            * @param minimumSize the new minimum size of this component
            * @see #getMinimumSize
            */
           public void setMinimumSize(Dimension minimumSize) {

      javax.swing.JScrollBar.setMinimumSize

      +    /**
      +     * Unlike most components, {@code JScrollBar} derives the minimum size from
      +     * the preferred size in one axis and a fixed minimum size in the other.
      +     * Thus, it overrides {@code JComponent.setMinimumSize} contract
      +     * that subsequent calls to {@code getMinimumSize} will return the
      +     * same value as set in {@code JComponent.setMinimumSize}.
      +     *
      +     * @param minimumSize the new minimum size of this component
      +     */
      +    public void setMinimumSize(Dimension minimumSize) {

      javax.swing.JScrollBar.setMaximumSize

           /**
      +     * Unlike most components, {@code JScrollBar} derives the maximum size from
      +     * the preferred size in one axis and a fixed maximum size in the other.
      +     * Thus, it overrides {@code JComponent.setMaximumSize} contract
      +     * that subsequent calls to {@code getMaximumSize} will return the
      +     * same value as set in {@code JComponent.setMaximumSize}.
      +     *
      +     * @param maximumSize the desired maximum allowable size
      +     */
      +    public void setMaximumSize(Dimension maximumSize) {

      javax.swing.JScrollBar.getMinimumSize

           /**
      -     * The scrollbar is flexible along it's scrolling axis and
      +     * Returns the minimum size for the {@code JScrollBar}.
      +     * The scrollbar is flexible along its scrolling axis and
            * rigid along the other axis.
      +     * As specified in {@code setMinimumSize} JScrollBar will derive the
      +     * minimum size from the preferred size in one axis and a
      +     * fixed minimum size in the other.
      +     *
      +     * @return the minimum size as specified above
            */
           public Dimension getMinimumSize() {

      javax.swing.JScrollBar.getMaximumSize

           /**
      -     * The scrollbar is flexible along it's scrolling axis and
      +     * Returns the maximum size for the {@code JScrollBar}.
      +     * The scrollbar is flexible along its scrolling axis and
            * rigid along the other axis.
      +     * As specified in {@code setMaximumSize} JScrollBar will derive the
      +     * maximum size from the preferred size in one axis and a
      +     * fixed maximum size in the other.
      +     *
      +     * @return the maximum size as specified above
            */
           public Dimension getMaximumSize() {

            psadhukhan Prasanta Sadhukhan
            mmma Marvin Ma (Inactive)
            Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: