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

JComboBox and JTree should only consume VK_ESCAPE when necessary

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.2.2, 1.3.0
    • client-libs
    • beta
    • generic, x86
    • generic, windows_2000



      Name: krT82822 Date: 01/22/2000


      (see also 4256046, 4138762, 4112282)

      java version "1.2.2"
      HotSpot VM (1.0.1, mixed mode, build g)

      In a dialog, the detection of an Escape key stroke by the user is
      needed in order to dismiss the dialog. Since the Cancel button usually
      does not have a mnemonic set, pressing Escape is the only fast way
      to cancel a dialog without using the mouse. Therefore, GUI components
      should not consume the VK_ESCAPE key event if it is not really
      necessary.

      However, at the moment,

      - JComboBox always consumes VK_ESCAPE. The correct behavior would be
        to only consume it when the associated popup menu is open (pressing
        Escape closes it). In all other cases, JComboBox should ignore the
        key event. This is the way it works under Windows.
      - JTree also always consumes VK_ESCAPE. The correct behavior would
        be to only consume it in order to cancel a node editing session.
        In all other cases, JTree should ignore the key event because
        there are times when it is desirable to use a JTree in a dialog
        - a directory chooser comes to mind.

      (There are similar problems with VK_ENTER and the activation of
       the default button but those are debatable.)

      ==

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
        
      public class EscapeBug extends JDialog {
        public EscapeBug() {
          super((Frame) null, true);
        
          // manually register escape action (see bug 4144757)
          getRootPane().registerKeyboardAction(new ActionListener() {
              public void actionPerformed(ActionEvent ev) {
                System.out.println("DEBUG: Dialog cancelled.");
                setVisible(false);
              }
            }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
            
          JPanel pnl = new JPanel();
          pnl.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
          pnl.setLayout(new GridBagLayout());
          
            JComboBox cmb = new JComboBox();
            cmb.addItem("Bug 1");
            cmb.addItem("Bug 2");
            cmb.setEditable(true);
            pnl.add(cmb, new GridBagConstraints(
              0, GridBagConstraints.RELATIVE, 1, 1, 1, 0,
              GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
              new Insets(0, 0, 6, 0), 0, 0));
            
            JScrollPane scp = new JScrollPane();
            
              JTree tree = new JTree();
              scp.getViewport().add(tree);
              
            pnl.add(scp, new GridBagConstraints(
              0, GridBagConstraints.RELATIVE, 1, 1, 1, 1,
              GridBagConstraints.CENTER, GridBagConstraints.BOTH,
              new Insets(0, 0, 17, 0), 0, 0));
              
            JPanel dlgButPnl = new JPanel();
            dlgButPnl.setLayout(new GridLayout(1, 0, 5, 0));
            
              JButton okBut = new JButton("OK");
              okBut.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ev) {
                    System.out.println("DEBUG: Dialog approved.");
                    setVisible(false);
                  }
                });
              dlgButPnl.add(okBut);
              getRootPane().setDefaultButton(okBut);
              
              JButton cancelBut = new JButton("Cancel");
              cancelBut.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ev) {
                    System.out.println("DEBUG: Dialog cancelled.");
                    setVisible(false);
                  }
                });
              dlgButPnl.add(cancelBut);
          
            pnl.add(dlgButPnl, new GridBagConstraints(
              0, GridBagConstraints.RELATIVE, 1, 1, 0, 0,
              GridBagConstraints.EAST, GridBagConstraints.NONE,
              new Insets(0, 0, 0, 0), 0, 0));
          
          setContentPane(pnl);
          
          setSize(new Dimension(384, 256));
          setLocation((getToolkit().getScreenSize().width - getWidth()) / 2,
            ((getToolkit().getScreenSize().height) - getHeight()) / 2);
          setVisible(true);
        }
        
        public static void main(String[] args) {
          new EscapeBug();
        }
      }
      (Review ID: 99849)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: