KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); dialog.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { //dosomething } }, stroke, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);