×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Hani Ibrahim
Added: Oct 25, 2018 4:15 PM
Views: 3526
  1.         /**
  2.      * Close dialog when ESC is pressed
  3.      *
  4.      * @param dialog Dialog frame which should be close by ESC
  5.      */
  6.     public static void addEscapeListener(final JDialog dialog) {
  7.         ActionListener escListener = new ActionListener() {
  8.  
  9.             @Override
  10.             public void actionPerformed(ActionEvent e) {
  11.                 //dialog.setVisible(false);
  12.                 dialog.dispose();
  13.             }
  14.         };
  15.  
  16.         dialog.getRootPane().registerKeyboardAction(escListener,
  17.                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
  18.                 JComponent.WHEN_IN_FOCUSED_WINDOW);
  19.     }