Mega Code Archive

 
Categories / Java / Swing JFC
 

JFrame that closes when someone presses the ESC key

/*  * jMemorize - Learning made easy (and fun) - A Leitner flashcards tool  * Copyright(C) 2004-2008 Riad Djemili and contributors  *   * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 1, or (at your option)  * any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.KeyStroke; import javax.swing.WindowConstants; /**  * A advanced JFrame that closes when someone presses the ESC key.  *   * @author djemili  */ public abstract class EscapableFrame extends JFrame {     public EscapableFrame()     {         // on ESC key close frame         getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(             KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); //$NON-NLS-1$         getRootPane().getActionMap().put("Cancel", new AbstractAction(){ //$NON-NLS-1$             public void actionPerformed(ActionEvent e)             {                 close();             }         });                  // on close window the close method is called         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);         addWindowListener(new WindowAdapter() {             public void windowClosing(java.awt.event.WindowEvent evt)              {                 close();             }         });     }          /**      * Is called when the frame is closed by pressing ESC or closing it by      * clicking on the close icon.      *       * @return True if the frame got closed; false otherwise.      */     abstract public boolean close(); }