Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4145134

JComboBox does not fire actionPerformed() on every ENTER key

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.0.1
    • client-libs
    • None
    • sparc
    • solaris_2.5.1

      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);
          }
      }

            tsantossunw Tom Santos (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: