Here you can find the source of setCancelAction(RootPaneContainer c, Action cancelAction)
JFrame
or JDialog
.
Parameter | Description |
---|---|
cancelAction | Action to be performed when the ESCAPE key is hit. Usually cancels the dialog. |
public static void setCancelAction(RootPaneContainer c, Action cancelAction)
//package com.java2s; /*/*from w ww. j a v a2s. c om*/ * Copyright (C) 2001-2013 Michael Koch (tensberg@gmx.net) * * This file is part of JGloss. * * JGloss 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 2 of the License, or * (at your option) any later version. * * JGloss 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 JGloss; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * */ import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.KeyStroke; import javax.swing.RootPaneContainer; public class Main { /** * Set up the escape keyboard shortcut for a <code>JFrame</code> or <code>JDialog</code>. * * @param cancelAction Action to be performed when the ESCAPE key is hit. Usually cancels * the dialog. */ public static void setCancelAction(RootPaneContainer c, Action cancelAction) { ((JPanel) c.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .put(KeyStroke.getKeyStroke("ESCAPE"), "cancelAction"); ((JPanel) c.getContentPane()).getActionMap().put("cancelAction", cancelAction); } }