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

[macosx] Editable JComboBox in Java 8 does not forward Enter key to default button

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • 9
    • 8u20, 8-pool, 9
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac 10.9.5

      A DESCRIPTION OF THE PROBLEM :
      In Java 7 and below, the Enter key event in a editable JComboBox is forwarded to trigger the default button action. In Java 8, default button is not triggered. Java 8 behavior is expected to be the same as Java 7.

      REGRESSION. Last worked in version 7u55

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_55"
      Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Have a JComboBox. Make it editable.
      2. Have a JButton in the same panel / frame. Make it the default button.
      3. With focus on the JComboBox, press Enter key.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      In Java 7 and below, the Enter key event is forwarded to trigger the default button action. In Java 8, default button is not triggered. Java 8 behavior is expected to be the same as Java 7.
      ACTUAL -
      When I press Enter key, default button is not triggered.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Container;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JOptionPane;
      import javax.swing.SwingUtilities;

      public class MyFrame extends JFrame {

          private static MyFrame me;

          public MyFrame() {
              Container c = getContentPane();
              c.setLayout(new BorderLayout());

              // Press Enter key with focus on this component:
              JComboBox jcb = new JComboBox(new String[]{"Option: One", "Option: Two"});
              jcb.setEditable(true);
              c.add(jcb, BorderLayout.CENTER);

              JButton jb = new JButton("Ok");
              jb.addActionListener(new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      JOptionPane.showMessageDialog(me, "Button pressed!");
                  }
              });
              SwingUtilities.getRootPane(c).setDefaultButton(jb);
              c.add(jb, BorderLayout.EAST);

              pack();
              setVisible(true);
          }

          public static void main(String[] arg) {
              SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      me = new MyFrame();
                  }
              });
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only workaround I can think of is having separate code paths for Java 8 and for earlier Java versions. For Java 8, I will have to manually capture the Enter key event and forward it to the button action.

            aniyogi Avik Niyogi (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: