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

Combobox editor prevents default button from working after keys are typed

XMLWordPrintable

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

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

      A DESCRIPTION OF THE PROBLEM :
      After keys are typed in an editable combobox, the first ENTER is ignored and default button action is not performed. Subsequent ENTER key presses are fine.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the code.
      When the dialog comes up, note that "Button 1" is the default button.
      Hit ENTER to prove to yourself that it works.
      Type in the combobox.
      Hit ENTER.
      Nothing happens.
      Hit ENTER again.
      Now, the default button works.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The default button should work when enter is pressed.
      ACTUAL -
      The default button does not work after user types in combo. Subsequent presses work (until the user again types in the combobox).

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.event.ActionEvent;
      import java.awt.*;

      public class TestButtons extends JFrame
      {
          private JButton fButton1;
          private JButton fButton2;
          private JComboBox fCombo1;
          private JComboBox fCombo2;
          
          public TestButtons()
          {
              fButton1 = new JButton(new MyAction("Button 1"));
              fButton1.setEnabled(true);
              fButton1.setDefaultCapable(true);

              fButton2 = new JButton(new MyAction("Button 2"));
              fButton2.setEnabled(true);
              fButton1.setDefaultCapable(true);
              
              String[] comboboxStrings = {""};
              fCombo1 = new JComboBox(new DefaultComboBoxModel(comboboxStrings));
              fCombo1.setEditable(true);
              
              fCombo2 = new JComboBox(new DefaultComboBoxModel(comboboxStrings));
              fCombo2.setEditable(true);
              
              JPanel panel = new JPanel(new BorderLayout());
              
              JPanel panel1 = new JPanel();
              JPanel panel2 = new JPanel();
              
              panel1.add(fCombo1);
              panel2.add(fCombo2);
              panel1.add(fButton1);
              panel2.add(fButton2);
              panel.add(panel1, BorderLayout.NORTH);
              panel.add(panel2, BorderLayout.CENTER);
              getContentPane().add(panel);
              
              getRootPane().setDefaultButton(fButton1);
              
              setLocation(300,300);
              setSize(300,300);
              setVisible(true);
          }
          
          
          public static void main(String[] args)
          {
              new TestButtons();
          }
          
          public void doStuff()
          {
             Toolkit.getDefaultToolkit().beep();
             System.out.println("doStuff - button pressed");
              
          }
          
          private class MyAction extends AbstractAction
          {
              public MyAction(String label)
              {
                  super(label);
              }
              public void actionPerformed(ActionEvent event)
              {
                  doStuff();
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Don't set a default button, set a KeyListener on the editor of the combobox. This is annoying though since now the user has no idea that hitting ENTER does something.

            draskinsunw Daniel Raskin (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: