3

I am creating an application in which any opened Jpanel or JFrame or Dialog should be closed on striking Escape button of the keyboard.

If I open any panel and direct hit Escape button that it is closed successfully without any problem.. but when I am trying to close it using Escape key after doing something in that JPanel or JFrame or Dialog, It is unable to close.

Please assist me if I am doing any thing wrong

Thanks in advance...

My method is

public static void addKeyBinding(JComponent c, final Object promptControl) {
         debugLogger.debug("Start Escape Key Binding ");

         Action escape = new AbstractAction() {

             {
                 putValue(NAME, "escape");
             }

             public void actionPerformed(ActionEvent e) {
                 try {
                     JComponent source = (JComponent) e.getSource();
                     Window window = SwingUtilities.getWindowAncestor(source);
                     window.dispose();
                     Dialog dialog = (Dialog) source.getFocusCycleRootAncestor();
                     dialog.dispose();
                     debugLogger.debug("source = " + source.getClass().getName() 
                             + "\n"
                             + "source.focusCycleRootAncestor = "
                             + source.getFocusCycleRootAncestor().getClass().getName());
                 } catch (Exception ex) {
                     errorLogger.error("Exception caught while closing the window." + x.toString());
                 }
             }
         };
         Object name = escape.getValue(Action.NAME);
         c.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), name);
         c.getActionMap().put(name, escape);
         debugLogger.debug("End Escape Key Binding ");
     }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38

2 Answers2

3

1) Use CardLayout instead of to create bunch of Top-Level Containers on Runtime

2) Re_use Top-Level Containers by removing its content, because these containers are presents in the memory

3) if you'll re_use JDialogs then you have to call only setVisible(true/fasle), better would be wrap visibilities to the invokeLater() to add this event to EDT

EDIT

window/dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
   .put(KeyStroke.getKeyStroke("ESC"), "myAction");
window/dialog.getRootPane().getActionMap().put("myAction", new AbstractAction() {...});
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • thanks mKorbal for the reply. can you provide more information that how to do it? I don't have any idea. – Ronak Jain Jan 30 '12 at 09:08
  • aaaah I see that, for listening from KeEyBindings you have to add RootPane and Focused, please see my edit – mKorbel Jan 30 '12 at 09:24
  • -1 for the bullets (random noise), +1 for the rootPane's keyBindings, that is zero until you edit and remove the noise :-) – kleopatra Jan 30 '12 at 09:44
  • 2
    c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), name); Now It's woking fine. – Ronak Jain Jan 30 '12 at 09:47
0
    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke("ESCAPE"), "closeTheDialog");
    getRootPane().getActionMap().put("closeTheDialog",
            new AbstractAction() {

                private static final long serialVersionUID = 8360999630557775801L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    //This should be replaced by the action you want to perform
                    cancel.doClick();
                }
            });

Was sorted with that. Thank to mKorbel

Community
  • 1
  • 1
tmwanik
  • 1,643
  • 14
  • 20