How do I increase or decrease the dropdown size of a JComboBox or let the dropdown adjust to a size so that it displays the largest element without cutting the element's text?

Doug Bell

I ran into this same problem. The popup menu for a JComboBox is sized based on the size of the JComboBox. In my case the problem surfaced when a JComboBox was displayed at less than its preferred width. The popup menu was also shrunk by the same amount.

The following class allows the JComboBox to be sized smaller by the layout manager by overriding getMinimumSize() and fools the look-and-feel by returning preferredSize() from getSize().

class ScaleableComboBox extends JComboBox
    ScaleableComboBox (ComboBoxModel model)

    ScaleableComboBox (Object[] data)

    // overridding getMinimumSize() is not needed to control the size
    // of the popup menu; it is to allow the combo box to be scaled
    // by a BoxLayout
    public Dimension getMinimumSize ()
        Dimension d = super.getMinimumSize();
        // allow combo box to scale to half default minimum size
        d.width /= 2;
        return d;

    // this is all you really need
    public synchronized Dimension getSize ()
        return getPreferredSize();

Amazingly enough, ComboBox.getSize() only appears to be called from the look-and-feel for the purpose of determining the size to use for the popup. The layout manager doesn't call it and nor does anything else as far as I can determine. This of course may not hold in the future, in which case returning something other than the actual size of the component from getSize() would likely cause undesired effects. However, I've used this in a commercial shrink-wrap app, and it survived both QA and "field testing" by users, albeit all that testing was under Swing 1.1.1 only.

As a note, I spent a fair amount of effort trying to identify when the call to getSize() from the look-and-feel was going to occur (it is called from javax.swing.plaf.basic.BasicComboPopup.show()). I was not able to identify when the call was going to happen, and therefore could not qualify when to return getPrefferedSize() and when to return the actual size. So this implementation always returns getPreferredSize().

[FAQ Manager Note] Be careful about using this in some layout managers -- you may get some "interesting" results, depending on the size the layout manager wants to use ;)

Make sure you test your use of it in your layouts by resizing the window smaller and larger. If it works, you should be fine.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.