7 Replies Latest reply on Aug 27, 2007 10:41 PM by 843806

    How to widen the drop-down list in a JComboBox

      Is there some way to set the width of the drop-down list of a JComboBox? Note, this is not the same thing as setting the width of the JComboBox itself (vis-a-vis setBounds() or setSize()). I've got my JComboBox set as wide as possible, but the thing is the items in the list are dynamic and user defined, and there is no way to predict the width of the widest item. Does anyone know how to do this?

        • 1. Re: How to widen the drop-down list in a JComboBox
          By default, the JComboBox dropdown is as wide as its widest element, so you should not have to make any modification. If you insert a very long item the drop down should take care of itself (assuming there is enough screen space to do so).

          However, if you are really interested in how it works, check out the BasicComboUI ...you will find a method like createPopup....check that out.

          Good luck!
          • 2. Re: How to widen the drop-down list in a JComboBox
            Unfortunately, my program seems to disagree ;). I'm looking at it right now, and the popup list is undeniably too narrow to show the longest item (a String that's only 1/4 showing). It seems to prefer being as wide as the JComboBox itself, and that I can only make so wide (the GUI is pretty packed as it is).

            I checked out BasicComboUI.createPopup(), but couldn't find anything I can use (maybe more insight on my part is needed). For one thing, it is protected, and for another, it returns a ComboPopup which is an interface whose methods are completely unrelated to anything I need. Any other suggestions at all (maybe some Swing settings that aren't set right?)
            • 3. Re: How to widen the drop-down list in a JComboBox
              • 4. Re: How to widen the drop-down list in a JComboBox

                checkout this forum thread:


                In the above thread, even the JExtendedComboBox is a little overkill. I chose a slightly different path:

                Create an adapter between the JComboBox and PopupMenuEvents that implements the PopupMenuListener interface and register it with the JComboBox via addPopupMenuListener().

                The adapter, upon receiving a PopupMenuEvent via the popupMenuWillBecomeVisible() method, then either resets the preferred width of the popup using the resizeComboPopup() method (from the thread above), or, alternatively, simply turns on the horizontal scroll bar of the JScrollPane that contains the popup.

                Hope that helps...
                • 5. Re: How to widen the drop-down list in a JComboBox
                  This is a few years too late, but if anyone else stumbles into here with the same question this can help. My solution is short to code (half of it is just comments) but semicomplex to explain, because it involves events firing other events with feedback prevention. Anyway, give your JComboBox a PopupMenuListener, and create a global boolean (this will be used to prevent feedback). The sneaky thing about this techniques is it extends the JComboBox, creates the JPopupMenu with the new length, and resets the JComboBox length by cancelling the popup, sneaking into popupMenuCanceled, and repopping from there! I amaze me!
                  //Popup state to prevent feedback
                  boolean stateCmb = false;
                  //Extend JComboBox's length and reset it
                  public void popupMenuWillBecomeVisible(PopupMenuEvent e)
                    JComboBox cmb = (JComboBox)e.getSource();
                    //Extend JComboBox
                    cmb.setSize(NEW_LENGTH, cmb.getHeight());
                    //If it pops up now JPopupMenu will still be short
                    //Fire popupMenuCanceled...
                    //Reset JComboBox and state
                    stateCmb = false;
                    cmb.setSize(OLD_LENGTH, cmb.getHeight());
                  //Show extended JPopupMenu
                  public void popupMenuCanceled(PopupMenuEvent e)
                    JComboBox cmb = (JComboBox)e.getSource();
                    stateCmb = true;
                    //JPopupMenu is long now, so repop
                  Message was edited by:

                  Message was edited by:
                  • 6. Re: How to widen the drop-down list in a JComboBox
                    I have implemented dlinderm's solution and it works except for one major problem:

                    If you click on a ComboBox but then click off of it (clicking somewhere else on the interface other than the ComboBox) without selecting anything, the extended width will never again display when clicking the ComboBox in the future. Instead, the ComboBox will always show the regular width.

                    Does anyone know how to get around this problem?
                    • 7. Re: How to widen the drop-down list in a JComboBox
                      Add this:
                      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                                stateCmb = false;