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

Size of JComboBox popup is wrong when it is updated via PopupMenuListener

    Details

      Description

      FULL PRODUCT VERSION :
      all versions form java 1.6.0_25 to java 1.7.0_25

      ADDITIONAL OS VERSION INFORMATION :
      Win XP 32 bit, Win 7 64 bit

      A DESCRIPTION OF THE PROBLEM :
      I added the PopupMenuListener to a JComboBox to update the size of the popup just before it is shown. When I click on the combo box to open
      the popup list, the elements from the popup list are truncated to the size of the combo box. It should use the new size computed from the elements length.

      This functionality is working when using java 1.6.0_24. It is a bug in all the newer versions.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      .......
      Vector<CustomListItems> customList = getCustomListItems();
      aCombo = new JComboBox(customList);
      aCombo .setEnabled(false);

      aCombo.setPrototypeDisplayValue( " XXXXXXXXXXXXXXXX " );
      aCombo.addPopupMenuListener(new PopupMenuListener() {
      @Override
      public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
      JComboBox box = (JComboBox) e.getSource();
      Object comp = box.getUI().getAccessibleChild(box, 0);
      if ((comp instanceof JPopupMenu)) {
      JPopupMenu popup = (JPopupMenu) comp;
      JComponent scrollPane = (JComponent) ((JPopupMenu) comp).getComponent(0);
      Dimension size = new Dimension();

      if (box.getItemCount() != 0) {
      FontMetrics metrics = ((JPopupMenu) comp).getFontMetrics(getFont());
      // Find which option is the most wide, to set this width as pop up menu's preferred!
      int maxWidth = 0;
      for (int i = 0; i < box.getItemCount(); i++) {
      if (box.getItemAt(i) == null)
      continue;
      int currentWidth = metrics.stringWidth(box.getItemAt(i).toString());
      if (maxWidth < currentWidth)
      maxWidth = currentWidth;
      }

      // +20, as the vertical scroll bar occupy space too.
      size.width = maxWidth + 20;
      size.height = scrollPane.getPreferredSize().height;
      scrollPane.setPreferredSize(size);
      scrollPane.setMaximumSize(size);
      }
      }
      }

      .........
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            • Assignee:
              ssadetsky Semyon Sadetsky (Inactive)
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: