Java JDialog Key Event installCloseKeyBinding(final JDialog dialog)

Here you can find the source of installCloseKeyBinding(final JDialog dialog)

Description

Install a close action binding to Ctrl-C/Command-C for the specified dialog.

License

Open Source License

Parameter

Parameter Description
dialog dialog, must not be null

Declaration

static void installCloseKeyBinding(final JDialog dialog) 

Method Source Code


//package com.java2s;
/*//from  w  w w . j a va2  s .co  m
    
dsh-midi-cytoscape3-app  Cytoscape3 app for MIDI networks.
Copyright (c) 2013 held jointly by the individual authors.
    
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
    
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with this library;  if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
    
> http://www.fsf.org/licensing/licenses/lgpl.html
> http://www.opensource.org/licenses/lgpl-license.php
    
*/

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;

public class Main {
    /**
     * Install a close action binding to <code>Ctrl-C</code>/<code>Command-C</code> for the specified dialog.
     *
     * @param dialog dialog, must not be null
     */
    static void installCloseKeyBinding(final JDialog dialog) {
        Action close = new AbstractAction() {
            @Override
            public void actionPerformed(final ActionEvent event) {
                dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
            }
        };
        int menuKeyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        KeyStroke closeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, menuKeyMask);
        JRootPane rootPane = dialog.getRootPane();
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(closeStroke, "close");
        rootPane.getActionMap().put("close", close);
    }
}

Related

  1. installEnterKey(final JDialog comp, final Action action)
  2. loadCommonKeyMap(final JDialog dialog)
  3. registerCloseAction(final JDialog dialog, KeyStroke keyStroke)
  4. setupOKCancelHotkeys(JDialog dlg, JButton btnOK, final JButton btnCancel)