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

JComboBox editor component fires an ActionEvent when focus is lost

XMLWordPrintable

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

      I created an editable ComboBox and ran into a problem with the combo-box's
      ActionListener (see bug 4145134). I then tried attaching an ActionListener
      to the combo-box's editor component. This solved the original problem but
      introduce a new problem. Now the ActionListener was being invoked when
      the combox-box lost focus.

      I was trying to use a combo-box for a search string in an editor.
      The goal is for the user to type in search string, hit return in the
      combo-box to find the first occurrence of the string. This was all
      fine and working. Now, if the user clicks in the editor, the combox-box
      loses focus, the ActionListener is invoked and second occurrence of the
      string is searched for. Not good.

      Here is small program that demostrates the problem. Run the program,
      type something into the combox box and the move the ouse outside of
      the program's frame. Notice that you get line of output everytime the
      mouse leaves the program's frame.

      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.JTextField;
      import com.sun.java.swing.JComboBox;

      public class ComboBoxBug2 {

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

          public static void main(String args[]) {
      JFrame frame = new JFrame("ComboBox Bug");
      Container contentPane = frame.getContentPane();
      JComboBox comboBox = new JComboBox();
      comboBox.setEditable(true);
      JTextField textField = (JTextField) comboBox.getEditor().getEditorComponent();
      textField.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: