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

Choice control doesn't respond properly after call to `removeAll`

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.7.0_04"
      Java <TM> SE Runtime Environment <build 1.7.0_04-b22>
      Java Hotspot<TM> Client VM <build 23.0.0-b21, mixed mode, sharing>

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      A Choice Component is populated with 3 items:
      1. "" (empty option/empty string)
      2. "test 1"
      3. "test 2"

      User selects option `2` "test 1" from the drop down which triggers an itemStateChange event. User then clicks on a button which programatically removes all the items from the Choice Component and repopulates the Choice Component with a new set of options.
      The new options are:
      1. "" (empty option/empty string)
      2. "new test 1"
      3. "new test 2"
      4. "new test 3"

      User is presented with the new list of options on the Choice Component and selects option `2` again from the drop down("new test 1" in this case) which never triggers the itemStateChange event. When the user selects any other option position other than the previously selected(before the call to `removeAll`) position the itemStateChange event is fired off as expected.

      This unexpected behavior did not occur in java 6.0. We expect the Choice Component to fire off the itemStateChange event because we invoked a `removeAll`.

      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Select value "test 1" from the dropdown. ("Handled! ctr: 1" is printed out to System.out)
      2. Click on the "remove all and add new items" button.
      3. Select value "new test 1" from the dropdown. (expected the callback to be triggered but itemStateChange event is never emitted and Handled message is never outputted to System.out)
      4. Selecting on any other option(option 1, 3 or 4) triggers the `itemStateChange` event ("Handled! ctr: 2" is printed out to System.out)
      5. After clicking on any other option clicking back to option 2 "new test 1" now triggers the `itemStateChange` event ("Handled! ctr: 3" is printed out to System.out)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      1. Select value "test 1" from the dropdown. ("Handled! ctr: 1" is printed out to System.out)
      2. Click on the "remove all and add new items" button.
      3. Select value "new test 1" from the dropdown. ("Handled! ctr: 2" is printed out to System.out)

      We expect that when a user selects "new test 1" from the drop down of the Choice Component that an `itemStateChange` to be triggered. This is because even though it is the same position as the previously selected value on the ChoiceComponent we had invoked `removeAll` on the Choice Component and refilled it with new items.
      ACTUAL -
      No `itemStateChange` event is triggered when selecting an item from the ChoiceComponent when selecting an item on the same position as what was previously selected before the call to `removeAll`


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.applet.Applet;
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.ItemEvent;
      import java.awt.event.ItemListener;

      public class ChoiceTest extends Applet {
          int ctr = 1;
          Choice choiceSample = new Choice();
          Button buttonSample = new Button("remove all and add new items");
          
          public void init() {
              setLayout(new GridLayout(5, 3));
              choiceSample.add("");
              choiceSample.add("test 1");
              choiceSample.add("test 2");
              choiceSample.addItemListener(new ChoiceTestListener(this));
              buttonSample.addActionListener(new ButtonTestListener(this));
              add(choiceSample);
              add(buttonSample);
          }
          
          public void choiceHandler(ItemEvent e) {
              System.out.println("Handled! ctr: " + ctr);
              ctr++;
          }
          
          public void buttonHandler(ActionEvent e) {
              choiceSample.removeAll();
              choiceSample.add("");
              choiceSample.add("new test 1");
              choiceSample.add("new test 2");
              choiceSample.add("new test 3");
          }
          
          public class ChoiceTestListener implements ItemListener {
              ChoiceTest adaptee;
              
              ChoiceTestListener(ChoiceTest adaptee) {
                  this.adaptee = adaptee;
              }
              
              public void itemStateChanged(ItemEvent e) {
                  adaptee.choiceHandler(e);
              }
          }
          
          public class ButtonTestListener implements ActionListener {
              ChoiceTest adaptee;
              
              ButtonTestListener(ChoiceTest adaptee) {
                  this.adaptee = adaptee;
              }

              public void actionPerformed(ActionEvent e) {
                  adaptee.buttonHandler(e);
              }
          }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              pchelko Petr Pchelko (Inactive)
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: