JDK-7171412 : awt Choice doesn't fire ItemStateChange when selecting item after select() call
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-05-24
  • Updated: 2015-04-14
  • Resolved: 2012-10-03
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 7 JDK 8
7u76Fixed 8 b61Fixed
Related Reports
Duplicate :  
Relates :  
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) 64-Bit Server VM (build 23.0-b21, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7600]

A DESCRIPTION OF THE PROBLEM :
The AWT Choice component no longer fires an ItemStateChange Event when selecting the previously selected item after code call to select(x).

i.e. we have a Choice with various string values.
The user selects a value and completes their current task.
The application clears the selection to a default state by calling Choice.select(0)
The user then selects the same value that was selected before the select call.
No ItemStateChange event is fired.

This meens that it is impossible to clear the state of a screen before additional input.

REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the supplied test class.
2. Select a value other than blank from the Choice.
3. A label at the top of the Frame will display the selected value (via the ItemStateChange event)
4. Click the Button, this will call select(0) on the Choice and clear the Label at the top of the Frame.
5. Select the same Choice item as in step 2.
6. Observe that the Label at the top of the Frame does not show the selected Choice value, the ItemStateChange Event was not fired, the ItemListener was not invoked.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
After calling Choice.select(x) and then using the Keyoard or Mouse to reselect the previously selected item, the ItemStateChange Event should be fired!
ACTUAL -
Selecting the previously selected item from the Choice after a call to Choice.select(x) does not fire the ItemStateChanged Event

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class ChoiceTest extends Panel
{
	private Choice choice;
	private Button selectBlankBtn;
	private Label status;
	
	void init()
	{
		setLayout(new BorderLayout());
		add(getChoice(), BorderLayout.CENTER);
		add(getSelectBlankBtn(), BorderLayout.SOUTH);
		add(getStatus(), BorderLayout.NORTH);
		setPreferredSize(new Dimension(100, 100));
	}
	
	private Label getStatus()
	{
		if (status == null)
		{
			status = new Label("");
		}
		return status;
	}
	
	private Button getSelectBlankBtn()
	{
		if (selectBlankBtn == null)
		{
			selectBlankBtn = new Button("Select Blank");
			selectBlankBtn.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent e)
				{
					getChoice().select(0);
					getStatus().setText("");
				}
			});
		}
		return selectBlankBtn;
	}
	
	private Choice getChoice()
	{
		if (choice == null)
		{
			choice = new Choice();
			choice.setPreferredSize(new Dimension(50, 50));
			choice.add(" ");
			choice.add("1");
			choice.add("2");
			choice.add("3");
			choice.addItemListener(new ItemListener()
			{
				@Override
				public void itemStateChanged(ItemEvent e)
				{
					if (e.getStateChange() == ItemEvent.DESELECTED)
						getStatus().setText(String.format("Deselected: %1$s", e.getItem()));
					if (e.getStateChange() == ItemEvent.SELECTED)
						getStatus().setText(String.format("Selected: %1$s", e.getItem()));
				}
			});
		}
		return choice;
	}
	
	public static void main(String[] args)
	{
		ChoiceTest test = new ChoiceTest();
		test.init();
		final Frame f = new Frame();
		f.setLayout(new GridBagLayout());
		f.add(test);
		f.pack();
		f.setSize(new Dimension(200, 200));
		f.setVisible(true);
		f.addWindowListener(new WindowListener()
		{
			@Override
			public void windowOpened(WindowEvent e)
			{
			}
			
			@Override
			public void windowIconified(WindowEvent e)
			{
			}
			
			@Override
			public void windowDeiconified(WindowEvent e)
			{
			}
			
			@Override
			public void windowDeactivated(WindowEvent e)
			{
			}
			
			@Override
			public void windowClosing(WindowEvent e)
			{
				f.dispose();
			}
			
			@Override
			public void windowClosed(WindowEvent e)
			{
				f.setVisible(false);
			}
			
			@Override
			public void windowActivated(WindowEvent e)
			{
			}
		});
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Unable to find a workaround in code.
The user can select any other item from the Choice and then reselect the previously selected item in order for the ItemStateChange Event to be fired correctly.

Comments
Verified in jdk8b115 Windows 7 Pro x64 Solaris 11 sparcv9 java version "1.8.0-ea" Java(TM) SE Runtime Environment (build 1.8.0-ea-b115) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b57, mixed mode) Tests test/java/awt/Choice/ItemStateChangeTest/ItemStateChangeTest.java ChoiceTest (from bug description) passed successfuly
11-11-2013

In the past all toolkits were aligned to post ItemStateChange when the same item is reselected. see: closed/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest Also see: http://java.net/jira/browse/MACOSX_PORT-544
08-10-2012