JDK-4145134 : JComboBox does not fire actionPerformed() on every ENTER key
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.0.1
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1998-06-03
  • Updated: 1998-06-03
  • Resolved: 1998-06-03
Related Reports
Relates :  
Relates :  
Description
I created an editable combox-box and attached an ActionListener
to it.  I ran the program, entered some text and hit the Return
key.  My ActionListener was invoked.  Then, without changing the
text in the combo-box, I hit Return again expecting that my ActionListener
would be invoked again, but it wasn't.  I discovered that the ActionListener
was only getting invoked when Return had been hit and the text had changed
since the last time the ActionListener had been invoked.

My real world scenario was an attempt to use a combo-box for a Find field
where I wanted to remember the previous find strings in the drop-down list.
I want the user to be able to hit Return to do the first find and to
subsequently hit Return to do repeated finds of the same string.

Here is a small program that demonstrates the problem.  Run the program,
type into the combo-box and then hit Return several times.  Notice that
you get just one line of output rather than a line per Return.

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.sun.java.swing.JFrame;
import com.sun.java.swing.JComboBox;
import com.sun.java.swing.JFrame;

public class ComboBoxBug1 {

    /**
     * Constructor.
     */
    public ComboBoxBug1 () {
    }

    public static void main(String args[]) {
	JFrame frame = new JFrame("ComboBox Bug");
	Container contentPane = frame.getContentPane();
	JComboBox comboBox = new JComboBox();
	comboBox.setEditable(true);
	comboBox.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		System.err.println("actionPerformed invoked");
	    }
	});
	contentPane.setLayout(new FlowLayout());
	contentPane.add(comboBox);
	frame.setSize(300, 300);
	frame.setVisible(true);
    }
}

Comments
WORK AROUND Attach to the ActionListener to the combo-box's "editor" (a JTextField). This did not work either due to then next bug that I will file (4145135). The problem is that this ActionListener is invoked when the textfield loses focus. To get this to work I had to attach a KeyListener to the JTextField and react to the Return key.
11-06-2004

EVALUATION This is not a bug. The default model only fires when the combo box's selection has changed. The supplied workaround is what you should do if you have special needs. See the solution in bug 4145135 to control the action events in the editor. tom.santos@eng 1998-06-03
03-06-1998