Java JRootPane set cancel Button

Description

Java JRootPane set cancel Button


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 setCancelButton(final JRootPane rp, final JButton b) {
        rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
        rp.getActionMap().put("cancel", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev) {
                b.doClick();//from w w  w  .ja v a 2  s .c o m
            }
        });
    }
}



PreviousNext

Related