JDK-7170787 : Choice control doesn't respond properly after call to `removeAll`
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2012-05-22
  • Updated: 2014-10-09
  • Resolved: 2013-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8Resolved
Related Reports
Relates :  
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 ----------

Comments
Reopening to change the resolution. No code changes were done here, the issue was already fixed by some other change set
05-09-2013

Not reproducible with JDK8. Reproducible with JDK7u21. Should we fix it in JDK7?
22-05-2013

EVALUATION Looks like a duplicat of 7157829. At least reverting of CR 6770017 solves the problem.
23-05-2012