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

JComboBox popup doesn't initially display selected item if it is set twice

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE PROBLEM :
      If JComboBox.setSelectedItem is called twice before it becomes visible, then on the first popup show its scroll does not contain current selected item.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run test code
      2. Click on the combo box to show the popup


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Current selected item should be visible in popup on the first open.
      ACTUAL -
      Current selected item is not visible in popup (you have to scroll up to see it).

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

      public class DropDownTest {

          public static void main(String[] args) {
              String[] months = new String[12];
              for (int i = 0; i < 12; i++)
                  months[i] = String.valueOf(i + 1);
              JComboBox ch = new JComboBox(months);
              ch.setSelectedItem(months[3]);
              ch.setSelectedItem(months[2]);
              JFrame frame = new JFrame();
              frame.add(ch, BorderLayout.CENTER);
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              frame.setLocationRelativeTo(null);
              frame.pack();
              frame.setVisible(true);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      You can create DefaultComboBoxModel first:
              DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(months);
              model.setSelectedItem(months[3]);
              JComboBox<String> ch = new JComboBox<>(model);
              ch.setSelectedItem(months[2]);


      FREQUENCY : always


      Attachments

        Activity

          People

            rmahajan Rajat Mahajan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: