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

JComboBox menu can display incorrectly when manipulated from PopupMenuListener

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 6u10
    • client-libs

    Description

      FULL PRODUCT VERSION :
      Identical behaviour on:
      1.6.0_06
      1.6.0_07
      1.6.0_10

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 5.1 (Build 2600.xpsp_sp3_gdr.080814-1236: Service Pack 3)

      also

      Linux clevo 2.6.25.14-69.fc8 #1 SMP Mon Aug 4 14:20:24 EDT 2008 i686 i686 i386 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      It can be necessary to create menu items for a popup menu just prior to it being displayed. This is a feature explicitly supported and implemented as part of feature request http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=4bebc5b1234a0c1a95df7b45ecd18?bug_id=4331058

      I include a simple test program that illustrates the use of the PopupMenuListener to create menu items, however the very first time the menu is clicked, it incorrectly displays the popup menu in the wrong size. It displays the combo box menu with the size that it had before the PopupMenuListener event occurred. As a result, the menu displayed after the items are created by the PopupMenuListener is incorrectly displayed with the wrong number of options, and with a very small scrollbar.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run this simple code below.

      The creation of the menu items happens in the PopupMenuListener event handler, and must continue to do that.

      The very first time you run the application, you only see one menu item when you click on the combo box.

      Subsequent times you see all five menu items.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The first menu displayed should be the same as the subsequent ones.

      The size of the popup should automatically be resized with changes to the JComboBox.

      ACTUAL -
      The PopupMenuListener is incorrectly displayed the first time you click, with the wrong number of options, and with a very small scrollbar.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Demo {

        public static void main(String args[]) {
          JFrame frame = new JFrame();
          frame.setTitle("Popup JComboBox");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setVisible (true);
          Container contentPane = frame.getContentPane();
          JComboBox box = new JComboBox();
          box.setModel( new DefaultComboBoxModel() );
          // the number of items added here determines the initial size of the popup, even after they've been replaced
          box.addItem("Test");

          final JComboBox comboBox = box;
          comboBox.addPopupMenuListener(new PopupMenuListener() {

              public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                // this is where it needs to create the menu items
                comboBox.removeAllItems();
                for( int i = 0 ; i < 5; i++ ) {
                  comboBox.addItem( "" + (i + 1));
                }
              }

              public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}

              public void popupMenuCanceled(PopupMenuEvent e) {}

          });
          contentPane.add(comboBox, BorderLayout.NORTH);
          frame.pack();
          frame.setSize (300, 300);
          frame.setVisible(true);
        }
      }

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

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: