JDK-6422966 : Editable JComboBox.setPrototypeDisplayValue: size can not be smaller than editor
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-05-08
  • Updated: 2023-01-13
Description
FULL PRODUCT VERSION :
java version "1.6.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59g)
Java HotSpot(TM) Client VM (build 1.6.0-beta-b59g, mixed mode)

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

A DESCRIPTION OF THE PROBLEM :
Same problem for earlier JDKs.

If a JComboBox is editable, the default width of the editor component (JTextField)
is 9 columns. The method setPrototypeDisplayValue can not be used
to make the JComboBox smaller than the editor.

Suggestions:
Allow setPrototypeDisplayValue to affect the size of the editor,
or document the setPrototypeDisplayValue method to
indicate its limitaions when used with an editable JComboBox .

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See sample code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would expect the setPrototypeDisplayValue method to work as documented.
ACTUAL -
setPrototypeDisplayValue can not be used to create a preferred size that is
smaller then the editor components prefered size.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package swing;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 * Editable JComboBox prerred size problem.
 */
public class TestJComboBox
{
    public static void main(String[] args)
    {
        JPanel contents = new JPanel(new FlowLayout());
        
        // This combo gets a proper peferred size.
        JComboBox box1 = new JComboBox(new String[] {"one", "two", "MMM"});
        box1.setPrototypeDisplayValue("MMM");

        // This editable combo does not get a proper peferred size.
        JComboBox box2 = new JComboBox(new String[] {"one", "two", "MMM"});
        box2.setEditable(true);
        box2.setPrototypeDisplayValue("MMM");
        
        contents.add(box1);
        contents.add(box2);
        
        
        
        JFrame frame = new JFrame("Test");
        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        frame.setContentPane(contents);
        frame.pack();
        frame.setSize(400,400);
        frame.setVisible(true);
    }
}

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

CUSTOMER SUBMITTED WORKAROUND :
For an editable combo, handle the preferred size by tweaking the default editor.
i.e.
((JTextField)box2.getEditor().getEditorComponent()).setColumns(3);

Comments
EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=16853
14-11-2006