Java JRootPane set action for Button

Description

Java JRootPane set action for Button


//package com.java2s;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;

import javax.swing.JRootPane;

import javax.swing.KeyStroke;

public class Main {
    public static void setDontSaveButton(final JRootPane rp, final JButton b) {
        rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "dontSave");
        rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_D, rp.getToolkit()
                        .getMenuShortcutKeyMask()), "dontSave");
        rp.getActionMap().put("dontSave", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev) {
                b.doClick();/*from w  ww  .  j a va  2 s  . c  o  m*/
            }
        });
    }
}



PreviousNext

Related