JDK-6688674 : Calling getPreferredSize on editable JComboBox messes up Nimbus L&F
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6u10
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-04-15
  • Updated: 2011-02-16
  • Resolved: 2008-06-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6u10 b26Fixed
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b14)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Calling getPreferredSize on an editable JComboBox, when using Nimbus Look & Feel, messes up the layout.

The submitted test program shows the messed up layout.  Leaving the method call to "edit_combo.getPreferredSize()" out solves the layout problem.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The submitted test program shows the messed up layout.  Leaving the method call to "edit_combo.getPreferredSize()" out solves the layout problem.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Editable combo box layout should be correct.
ACTUAL -
Editable combo box height is messed up.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;

public class TestNimbus {
  public static void main( String[] args ) throws Exception {
    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );

    Object[] data = {"Jan", "Piet", "Joris", "Korneel"};

    JComboBox combo = new JComboBox( data );

    JComboBox edit_combo = new JComboBox( data );
    edit_combo.setEditable( true );

    //This line causes layout to be messed up !!!  If left out, it works.
    edit_combo.getPreferredSize();

    JToolBar tb = new JToolBar( );
    tb.add( combo );
    tb.add( edit_combo );

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    Container content = frame.getContentPane();
    content.setLayout( new BorderLayout() );
    content.add( tb, BorderLayout.NORTH );

    frame.pack();
    frame.setLocationRelativeTo( null );
    frame.setVisible( true );

  }
}

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

Comments
EVALUATION This was a complex bug to track down. What happens is Nimbus though Synth sets the border on the editor JTextField which causes the preferred size of the editor to change. BasicComboBoxUI caches the size of the editor and only recomputes it on a few events. Those did not include the case when the editor's border has changed. A fix for this needs to be done in BasicComboBoxUI as caching is internal private the change has to be done in there. Though I think this is a general bug with BasicComboBoxUI and could effect all LAFs. The reason why Nimbus hit this bug is Synth does not apply styles to components till they are added to a parent component and validated. Nimbus also has different insets(Border) for JTextField when it is used in a Combo to standard. So if you call getPreferredSize() on JCombo box before that point then it will cache the incorrect size information almost for ever(Unless model or editor or couple other things change).
28-05-2008

EVALUATION I didn't reproduce it, the reason of this problem may be the fact that provided test case works with Swing from the wrong thread assigned to Nimbus owner
17-04-2008