Java 6 JComboBox Regression?
Today I decided to try out my application with the Java 6 JRE. I was quite impressed until I found that the JComboBox is no longer firing action events when the user selects the same item from the list. Action events are fired in JRE 1.4 and 1.5, but it breaks in 1.6.
Here is a simple test case:
-
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ActionTest extends JFrame {
public ActionTest() {
String[] items = { "item1", "item2", "item3"};
JComboBox jComboBox = new JComboBox(items);
jComboBox.addActionListener(new ActionHandler());
getContentPane().add(jComboBox);
pack();
setVisible(true);
}
public static void main(String[] args) {
ActionTest actionTest = new ActionTest();
}
class ActionHandler implements ActionListener {
public final void actionPerformed(final ActionEvent e) {
System.out.println("Action Event Fired");
}
}
}
When the above code is run under java 1.4 or 1.5, an action event is fired even when the user selects the same item from the list. According to the docs this is the correct behaviour. However, when the above code is run under 1.6, no action events are fired when the user selects the same item from the list. This seems like a rather serious regression. Has anyone else experienced this?
Robert
[1489 byte] By [
tyriea] at [2007-11-26 12:41:04]

# 1
Use the "Code Formatting Tags" when posting code so the code retains its original formatting.
> When the above code is run under java 1.4 or 1.5, an action event is fired even when the user selects the same item from the list.
I assume you are talking about a mouse click, then yes it does fire an ActionEvent every time an item is clicked
It does not fire an ActionEvent if you open the combo box and then simply use the enter key.
I'm using JDK1.4.2 on XP.
# 2
> I'm using JDK1.4.2 on XP.
Yes, it works with 1.4. However, I'm referring to a possible regression in 1.6. When the code is run against JRE 1.6, no action event is fired if the same item is selected a second time. For example, if I select "item1" with the mouse, an action event is fired. However, if I select "item1" again, no action event is fired.
In JRE 1.4 and 1.5 events are fired with every selection, in 1.6 an event is fired only when the new selection is different than the previous selection.
# 4
When initializing and after every selection I do
setSelectedItem(null)
,
so I get actions, BUT the ugly side is an empty textfield.
Seems, the action only is fired after a selection CHANGE (as default, the sectedItemIndex is zero, which means no chance with only one alternative),
but I would have expected this new "feature" to be documented.
Up to now, I didn't find any comment (probably the answer is there, but what's the right question?).
Good luck
Guenter