Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4743225

Size of JComboBox list is wrong when list is populated via PopupMenuListener

XMLWordPrintable

    • b105
    • x86
    • linux, windows_xp
    • Verified

        Name: jk109818 Date: 09/06/2002


        FULL PRODUCT VERSION :
        java version "1.4.1-rc"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
        Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)

        FULL OPERATING SYSTEM VERSION :
        Microsoft Windows XP [Version 5.1.2600]

        A DESCRIPTION OF THE PROBLEM :
        I'm exploiting the new capability in JDK 1.4 to add a
        PopupMenuListener to a JComboBox to populate the list just
        before it is shown. When I click on the combo box to open
        the popup list, the contents of the popup list looks fine,
        but the size of the list is wrong. The size of the list is
        apparently calculated from the previous number of entries in
        the combo box model. It should use the new count of entries
        instead. To get the popup list to size correctly, you must
        close the popup list and open it again.

        I reported this problem once before on 6/17/2002. It was
        assigned an internal review ID of 153748. But I haven't
        seen any further progress, and the bug doesn't show up in
        the bug database, so I'm submitting it again.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Run the provided demonstration program.
        2. Click on the arrow to open the popup list for the combo box.
        3. Observe that the list is sized to show three items, which
        is the OLD size of the list.
        4. Close the popup list and open it again to observe that
        the list is now sized for 8 items, the default maximum list
        size.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        When opening the combo box, it is sized for 3 items, but the
        list really contains 10 items. It should size the list for
        8 items, the default maximum list size.

        REPRODUCIBILITY :
        This bug can be reproduced always.

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

        class ComboBug extends AbstractListModel implements ComboBoxModel,
        PopupMenuListener {
           JFrame f;
           Object selected;
           Object[] items;

           public static void main(String[] args) {
              new ComboBug();
           }

           ComboBug() {
              f = new JFrame("Combo demo");
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              items = new Object[] {"First", "Second", "Third"};
              setSelectedItem(items[0]);

              JComboBox combo = new JComboBox(this);
              combo.addPopupMenuListener(this);
              f.getContentPane().add(combo, BorderLayout.CENTER);
              f.pack();
              f.show();
           }

           public int getSize() {
              return items.length;
           }

           public Object getElementAt(int index) {
              return items[index];
           }

           public void setSelectedItem(Object obj) {
              selected = obj;
              fireContentsChanged(this, -1, -1); // Same as what DefaultComboBoxModel fires
           }

           public Object getSelectedItem() {
              return selected;
           }

           public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
              int oldSize = items.length;
              items = new Object[] {"One", "Two", "Three", "Four", "Five", "Six",
        "Seven", "Eight", "Nine", "Ten"};
              fireIntervalRemoved(this, 0, oldSize - 1);
              fireIntervalAdded(this, 0, items.length - 1);
              setSelectedItem(items[0]);
           }

           public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
           public void popupMenuCanceled(PopupMenuEvent e) {}
        }

        ---------- END SOURCE ----------
        (Review ID: 164152)
        ======================================================================

              alexp Alexander Potochkin (Inactive)
              jkimsunw Jeffrey Kim (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: