4 Replies Latest reply on Nov 26, 2003 4:03 AM by 807587

    JDialog says ESC key is already registered--where?

    807587
      The javadocs for JDialog indicate that a key binding already exists for JDialog for the ESC key:

      http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/doc-files/Key-Metal.html#JDialog

      ...but I don't know where to look for this, since JDialog is not a JComponent and hence does not have an InputMap associated with it.

      Additionally, pressing ESC does not seem to do anything in my new JDialog class.

      Where can I find the key bindings for the JDialog class, and, to cut to the chase, in JDK 1.4.2 do I actually need to add an ESC key binding in some fashion to dismiss the dialog box?

      Thanks,
      Laird
        • 1. Re: JDialog says ESC key is already registered--where?
          807587
          Add an ESC key binding to the root pane of your dialog to close the dialog. The best way to do it is, create MyDialog by extending JDialog and add a key binding and an action to close the dialog. Then use MyDialog through out your app where ever you want this extra bit of functionality.
          • 2. Re: JDialog says ESC key is already registered--where?
            807587
            Add an ESC key binding to the root pane of your dialog
            to close the dialog.
            Yes, thank you; I am aware of this potential solution. My question is more related to the fact that the documentation says that such a binding already exists for JDialog itself, but I have no idea how to verify or disprove this.

            Thanks for your input,
            Laird
            • 3. Re: JDialog says ESC key is already registered--where?
              807587
              Most (possibly all) of the default key bindings are defined in the XXXLookAndFeel class for whichever L&F you're using. In this case, they're set first in BasicLookAndFeel, then possibly overridden in MetalLookAndFeel. But there are no settings for JDialogs in either of those classes, not for key bindings or anything else. And, as you pointed out, pressing ESC does not close the dialog. It looks to me like whoever wrote that part of the docs just copied the entry for JOptionPane and changed the name to JDialog.

              Here's one way to implement psai's suggestion:
                protected void dialogInit()
                {
                  super.dialogInit();
              
                  JLayeredPane layeredPane = getLayeredPane();
                  layeredPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                      .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close-it");
                  layeredPane.getActionMap().put("close-it",
                      new AbstractAction()
                      {
                        public void actionPerformed(ActionEvent evt)
                        {
                          JDialog.this.hide();
                        }
                      });
                }
              I use the LayeredPane instead of the RootPane to avoid a minor conflict with PopupMenu key bindings. Also note that this just hides the dialog; if you want to destroy it, you can change hide() to dispose().
              • 4. Re: JDialog says ESC key is already registered--where?
                807587
                Most (possibly all) of the default key bindings are
                defined in the XXXLookAndFeel class for whichever L&F
                you're using.
                Nope; checked that; besides, they'd be in an InputMap, which of course JDialog lacks.
                It
                looks to me like whoever wrote that part of the docs
                just copied the entry for JOptionPane and changed the
                name to JDialog.
                Good sleuthing; I bet that's what happened.
                new AbstractAction()
                {
                public void actionPerformed(ActionEvent
                tionEvent evt)
                {
                JDialog.this.hide();
                }
                });
                Thank you. You might consider posting a WINDOW_CLOSING event instead, which will also allow the dialog box to be dismissed by the "X" box in the same manner.

                Thanks for your input,
                Laird