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

JComboBox editor component fires an ActionEvent when focus is lost

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.0.2
    • client-libs
    • None
    • sparc
    • solaris_2.5.1

      I originally filed this as bug 4145135 and I am now re-filing
      the problem as an RFE. In an email exchange with Tom Santos
      he explained that the motivation for firing an ActionEvent on
      focus lost is for when a combo box is part of a form and the
      user is tabbing betweeen controls. I agree that this is a
      sensible default.

      My RFE here is for a property that controls this behaviour. I would like
      to see a property that can be set such that losing focus does not
      fire an ActionEvent. The workaround requires me to know
      too much about the implementation of the combo box. Can I really be
      sure that the Editor is a JTextField? To do it correctly, I should
      implement my own editor so I know what kind of control it is. This
      is requiring far too much just so I can stop getting an ActionEvent
      when the focus is lost.


      The original description:

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

            mdavidsosunw Mark Davidson (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: