JComboBox won't select First item in list since Java 1.6
I've been running a java app for several years in a production environment. Recently I allowed the J2SE Runtime Environment 6 Update 1 to be installed.
Ever since, I'm unable to select the first (top) item in a JComboBox drop-down list. If I switch out the JRE back to jre1.5.0_11 (or earlier) it all works again using the same jar files without re-compiling.
Has anyone else seen this behavior or know if there is a solution to it.
Thanks.
Lynn
[478 byte] By [
Lynn46a] at [2007-11-27 3:51:06]

# 1
Looks like http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4480348, but that bug is closed. Could it be back? You could always check with jre 1.4 to check...
# 2
Thanks for the reply. I took the code presented in the bug report and modified it to show the problem.
Run it with both a 1.5 and a 1.6 JVM using a shell window so you can see Stdout:
java -version:1.6 ComboTest
java -version:1.5 ComboTest
Observe the fact that the event triggers every time (including the first click) with 1.5 but only when the selected content is different under 1.6. in fact you can repeatedly click the 'first one' over and over and the event never fires under 1.6.
import javax.swing.*;
import java.awt.event.*;
public class ComboTest extends JDialog implements ActionListener
{
JComboBox combo;
public static void main( String[] args )
{
System.out.println( System.getProperty( "java.version" ));
ComboTest ct = new ComboTest();
}
public ComboTest()// constructor
{
combo = new JComboBox();
combo.addItem( "First one" );
combo.addItem( "Second one" );
combo.addItem( "Third one" );
combo.addActionListener( this );
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( combo );
frame.pack();
frame.setVisible(true);
}
public void actionPerformed( ActionEvent aoEvent )
{
if( aoEvent.getSource() == combo )
{
System.out.println( "Event Triggered" );
}
}
}
# 3
> Observe the fact that the event triggers every time
> (including the first click) with 1.5 but only when
> the selected content is different under 1.6. in fact
> you can repeatedly click the 'first one' over and
> over and the event never fires under 1.6.
One could argue that this behaviour is no bug. Why should an actionPerformed be triggered if there is no change of selected item? If you need to trace the mouse clicks, you have to add your own MouseListener, I believe.
I tried the code you supplied, but was not able to reproduce problems with the first/top item usiong JDK1.6.
# 4
Very odd that you can't reproduce it... I've got about two dozen Win-XP machines here at work that show the problem every time. I've had to roll back to 1.5.
What I'm showing is that there is a difference in the way the two JVM's react to the identical byte-code.
I'll download the 1.6 documentation and see if they've added a method to allow JComboBox to produce the prior behavior.
Lynn
# 5
I'm using 1.6.0_01 on winXP SP2. Have no jre1.5, so I cannot check for differences, but I just tried once more to make sure, and I am able to select the first/top item at any time without problems, and the actionPerformed method is invoked every time I select an item that was not already selected.
Maybe someone else can try on their machines to shed some light on this?
# 6
You stated: I am able to select the first/top item at any time without problems, and the actionPerformed method is invoked every time I select an item that was not already selected.
Maybe I'm having a problem describing the problem clearly. My bad...
With 1.5 the actionPerformed method triggers EVERY time regardless of what was selected prior to it. If I click the top one six times in a row, I get six event triggers.
With 1.6 I only see the event trigger when the newly selected value differs from the previous value that displays in the textbox portion of the combobox. Thus, if I start the test program and click the second one on the list 6 times in a row, I only get the event to trigger once.
# 7
No problem, your explanation was nice and clear. But I still believe this is more correct in 1.6. No need to trigger an action if the selection is still the same. If you need to know that users want to select the same item once more, have an update-button handle it. Isn't that more
# 8
Yes, we are also seeing the same problem as mentioned by Lynn. We had to fall back to previous version because of this issue. I don't know wether this is the right/intended behaviour, but I am sure it is not behaving the way it used to prior to 1.6. May be we are doing something wrong?
# 9
I have encountered the same problem, again on an application that has been running for years. However, my app's existing code picks up a click followed by mouse up in the JComboBox and processes that OK. So a click and no drag works on my App. So the only problem I have is that no event is generated if the user clicks down on the JComboBox, drags to select something else, changes their mind and comes back to the same selected item. Then no event is generated whereas if they select a different item an event is genereated and processed.
The previous implementation was slightly dodgy on a fast machine in that it was easy to let go of the mouse whilst selecting and hence generate unwanted events. This problem seems to have gone in 1.6. Thus overall the new implementation may be better but I'll have to re-write the user manual !