JDK-6510914 : JScrollBar.getMinimumSize() breaks the contract of JComponent.setMinimumSize()
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0u6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 2007-01-09
  • Updated: 2017-05-23
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