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

The editable JComboBox doesn't handles focus events

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The editable JComboBox doesn't handles focus listeners.
      If we make that as non-editable, it is handling focus listeners.

      Here is my code
      [code]
      import java.awt.GridBagLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.FocusAdapter;
      import java.awt.event.FocusEvent;
      import java.awt.event.FocusListener;

      import javax.swing.ComboBoxEditor;
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;

      public class TestCombo extends JFrame
      {
        JComboBox cmb;
        
        public TestCombo()
        {
          cmb = new JComboBox(new String[]{"Camera1", "Camera2", "Camera3", "Camera4"});
          
          setLayout(new GridBagLayout());
          add(cmb);
          add(new JButton("Test"));
          
          cmb.setEditable(true);
          
          cmb.getEditor().addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
              System.out.println(cmb.getSelectedItem() + " " + cmb.getSelectedIndex());
              System.out.println(cmb.getEditor().getItem());
            }
          });
          
          cmb.addFocusListener(new FocusListener(){
            public void focusLost(FocusEvent e)
            {
              System.out.println("Focus lost");
            }

            public void focusGained(FocusEvent e)
            {
              System.out.println("Focus gained");
            }
          });
          
          setTitle("Test Combo");
          setSize(300, 300);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setVisible(true);
        }
        
        public static void main(String[] args)
        {
          new TestCombo();
        }
      }
      [/code]


      REPRODUCIBILITY :
      This bug can be reproduced always.
      I wrote more friendly test version:

      --- Source begin ---
      import java.awt.GridBagLayout;
      import java.awt.event.*;
      import javax.swing.*;

      public class Test extends JFrame {
          JComboBox cmb;
          JButton btn;

          public static void main(String[] args) throws Exception {
              SwingUtilities.invokeAndWait(new Runnable() {
                  public void run() {
                      new Test();
                  }
              });
          }

          public Test() {
              cmb = new JComboBox(new String[]{"one", "two"});
              btn = new JButton();
              toggleEditable();
              
              getContentPane().setLayout(new GridBagLayout());
              getContentPane().add(cmb);
              getContentPane().add(btn);

              cmb.addFocusListener(new FocusListener() {
                  public void focusLost(FocusEvent e) {
                      System.out.println("Focus lost");
                  }

                  public void focusGained(FocusEvent e) {
                      System.out.println("Focus gained");
                  }
              });

              btn.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      toggleEditable();
                  }
              });

              setTitle("Test focus listener for Combo");
              setSize(300, 300);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              setVisible(true);
          }

          private void toggleEditable() {
              if (cmb.isEditable()) {
                  cmb.setEditable(false);
                  System.out.println("Combo box is uneditable");
                  btn.setText("Make editable");
              } else {
                  cmb.setEditable(true);
                  System.out.println("Combo box is editable");
                  btn.setText("Make uneditable");
              }
          }
      }
      --- Source end ---

      Test strategy:
      - Try change focus owner by the TAB key pressing
      - Try change the Combo editable feature by pressing the Button
      - See that editable combo doesn't get focusGained and focusLost events

            shickeysunw Shannon Hickey (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: