United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6688674 Calling getPreferredSize on editable JComboBox messes up Nimbus L&F
JDK-6688674 : Calling getPreferredSize on editable JComboBox messes up Nimbus L&F

Details
Type:
Bug
Submit Date:
2008-04-15
Status:
Resolved
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2008-06-09
Component:
client-libs
OS:
windows_xp
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6u10
Fixed Versions:
6u10 (b26)

Related Reports

Sub Tasks

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

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
                                     
2008-04-17
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).
                                     
2008-05-28



Hardware and Software, Engineered to Work Together