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

Editable JComboBox may require two Enter keys to fire default button

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 1.4.2, 5.0
    • client-libs
    • beta
    • x86
    • windows_2000, windows_xp


      Name: ks84122 Date: 01/28/2003


      Modify selected item in editable JComboBox in a JFrame with a default button. Pressing Enter once does nothing visible, pressing Enter a second time fires the default button.

      (The scenario as I see it follows. BasicComboBoxUI sees modified value does not match selected item so does not fire "enterpressed". JComboBox does not distinguish between popup visible or not in behavior so just fires "comboBoxEdited" which changes the selection. On next Enter, selected item does match visible value so now does fire "enterpressed".)

      Run sample code below. If hit Enter without modifying content of JComboBox, Enter closes the dialog by firing the default button. If modify content, have to hit Enter twice to close the dialog.

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;


      public class Frame1 extends JFrame {
        JButton jButton1 = new JButton();
        JComboBox jComboBox1 = new JComboBox(new String[] {"one", "two"});

        public Frame1() {
          try {
            jbInit();
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }

        private void jbInit() throws Exception {
          jComboBox1.setEditable(true);

          jButton1.setText("OK");
          jButton1.setDefaultCapable(true);
          jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));

          getContentPane().add(jButton1, BorderLayout.SOUTH);
          getContentPane().add(jComboBox1, BorderLayout.NORTH);
          getRootPane().setDefaultButton(jButton1);
        }

        void jButton1_actionPerformed(ActionEvent e) {
          setVisible(false);
          dispose();
        }

        class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
          Frame1 adaptee;

          Frame1_jButton1_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
          }
          public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
          }
        }

        public static void main(String[] args) {
          Frame1 frame = new Frame1();
          frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
          frame.pack();
          frame.setLocation(new Point(200, 200));
          frame.show();
        }

      }
      (Review ID: 180316)
      ======================================================================

            alexp Alexander Potochkin (Inactive)
            ksoshals Kirill Soshalskiy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: