JDK-4713519 : 1.4 REGRESSION: JComboBox behaves differently in jdk1.3.1 and jdk1.4
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-07-11
  • Updated: 2002-10-19
  • Resolved: 2002-10-19
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.
Other
1.4.2 mantisFixed
Description

Name: gm110360			Date: 07/11/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION : Windows NT Version 4.0


A DESCRIPTION OF THE PROBLEM :

Steps.

1. remove all elements in a JCombobox.
2. add new items to the JComboBox.
3. set selected index of combo box as zero.

Result.

in jdk1.3.1 item state change event will be fired always.
in jdk 1.4.0 itemStateChange even will be fired only if the
display string is different from the display String which
was selected before removing all items in the combobox



REGRESSION.  Last worked in version 1.3.1

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

1. Load a JComboBox with a list of string values. set
selected index as 0. itemStateChanged event will be
genereated.

2. Remove all items in combo box using combo.removeAllItems

3. Load the the combobox again with the same list of
strings used in step 1. set selected index as 0. This time
itemStateChanged will not be fired in jdk1.4.0 , but in
jdk1.3.1 it is fired.

EXPECTED VERSUS ACTUAL BEHAVIOR :

The itemSstateChanged event should be fired.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------




import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.util.* ;

/**
* author Abhilash K Bhaskaran
* 
* Click on "reload" button multiple times in jdk1.3.1 and
* jdk1.4.0 to see the result.
*/
public class TEST extends JFrame{
	private JButton reload ;
	private JComboBox combo ;

	public void init(){
	    combo = new JComboBox() ;
		combo.addItemListener( new ItemListener(){
				public void itemStateChanged( ItemEvent event ){
				  if (null != combo.getSelectedItem() &&
				  		event.getStateChange() ==
ItemEvent.SELECTED){
					System.out.println("S:"+
combo.getSelectedItem()) ;
				  }
				}
			} );
		reload = new JButton("reload") ;
		reload.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					loadCombo() ;
				}
			}) ;
		JPanel panel = new JPanel(new BorderLayout()) ;
		panel.add(combo,BorderLayout.NORTH) ;
		panel.add(reload,BorderLayout.SOUTH) ;
		getContentPane().add(panel) ;
	}
	public void loadCombo(){
		combo.removeAllItems() ;
		for (int iCount = 0 ; iCount <10 ; iCount++){
			String strItem = "Item-"+iCount ;
	    	combo.addItem(strItem) ;
		}

		try{
			combo.setSelectedIndex(0) ;
		}catch(Exception e){}
	}
	public static final void main(String args[]){
		TEST app = new TEST() ;
		app.init() ;
		app.setSize(100,100) ;
		app.setVisible(true) ;
		app.loadCombo() ;
	}
}
---------- END SOURCE ----------

Release Regression From : 1.3.0_03
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 139926) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis FIXED IN: mantis INTEGRATED IN: mantis
14-06-2004

EVALUATION The semantics of firing an itemStateChanged event changed in 1.4 so that the event will be fired only when the selected item changes. The actionEvent will always be fired. However, the removal of items and repopulating the list touches a similar bug reported in 4712013 in which the cached selected item in JComboBox is not cleared when removeAllItems is called. This could be the problem that the submitter is experiencing. Not a showstopper for Hopper but will consider it for Mantis. ###@###.### 2002-07-11 The fixes to 4712013 and 4741130 will enable the itemRemoved method and force the selection events. Unfortunately, multiple events are fired indescriminately since the check for the item selection change is not made. An additional fix would be to check to see that the item selection has been made before calling selectedItemChanged and the DefaultComboBox.addElement should only fire the intervalAdded event after a selection has been made. ###@###.### 2002-10-02
02-10-2002