United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6510914 : JScrollBar.getMinimumSize() breaks the contract of JComponent.setMinimumSize()

Details
Type:
Bug
Submit Date:
2007-01-09
Status:
Open
Updated Date:
2017-05-23
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
solaris_2.5.1
Sub-Component:
javax.swing
CPU:
sparc
Priority:
P4
Resolution:
Unresolved
Affected Versions:
5.0u6
Targeted Versions:

Related Reports

Sub Tasks

Description
The javadoc contract for JComponent.setMinimumSize(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 - it always returns a minimum size derived from the preferred size even if you have previously called setMinimumSize().

Code to reproduce:

JScrollBar bar = new JScrollBar( SwingConstants.HORIZONTAL );
bar.setMinimumSize( new Dimension( 75, 0 ) );
System.out.println( bar.getMinimumSize() );

Expected: Prints "java.awt.Dimension[width=75,height=0]"
Actual: Prints "java.awt.Dimension[width=5,height=17]"

The code for JScrollBar.java contains this comment above the implementation of getMinimumSize():

// PENDING(hmuller) - the next three methods should be removed

Indeed, removing those three methods would fix this bug.

                                    

Comments
EVALUATION

At this point it's undoubtedly the case that some folks are depending upon these implementations. As such, we can't really remove them. Instead, the implementations should check if the minimum/maximum size has been set, and if so honor it.
                                     
2007-01-16
WORK AROUND

Create a subclass of JScrollBar like so:

JScrollBar bar = new JScrollBar( SwingConstants.HORIZONTAL ) {
  public Dimension getMinimumSize()
  {
    // If setMinimumSize() was called, use Component.minimumSize().
    if ( isMinimumSizeSet() ) return super.minimumSize();
    // Else, fall back to the (broken) superclass implementation.
    return super.getMinimumSize();
  }
};
                                     
2007-01-09



Hardware and Software, Engineered to Work Together