Example usage for javax.swing AbstractAction AbstractAction

List of usage examples for javax.swing AbstractAction AbstractAction

Introduction

In this page you can find the example usage for javax.swing AbstractAction AbstractAction.

Prototype

public AbstractAction() 

Source Link

Document

Creates an Action .

Usage

From source file:Main.java

public static void closeOnEscape(final JFrame frame) {
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            frame.setVisible(false);//  w w w.  ja  v a2 s.  c  o  m
            frame.dispose();
        }
    };
    JRootPane rootPane = frame.getRootPane();
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);
}

From source file:Main.java

public static void closeOnEscape(final JDialog parent) {
    parent.getRootPane().getActionMap().put("close_on_escape", new AbstractAction() {
        @Override/*ww w .ja va 2s  .c  o m*/
        public void actionPerformed(ActionEvent e) {
            parent.dispose();
        }
    });
    parent.getRootPane().getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close_on_escape");
}

From source file:Main.java

public static void teclaAtalhoRequestFocus(JComponent pai, final Component component, String nomeAcao,
        String... atalhos) {/* w ww. j a  va 2  s  .co m*/
    associaTeclaAtalho(pai, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            component.requestFocusInWindow();
        }
    }, nomeAcao, atalhos);
}

From source file:Main.java

public static void installEscapeCloseOperation(final JDialog dialog) {
    Action dispatchClosing = new AbstractAction() {
        public void actionPerformed(ActionEvent event) {
            dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
        }// w  w w. j av a  2s. c o  m
    };
    JRootPane root = dialog.getRootPane();
    root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_KEY_STROKE, ESCAPE_KEY);
    root.getActionMap().put(ESCAPE_KEY, dispatchClosing);
}

From source file:Main.java

/**
 * Binds an action to the dialog so when the user presses the ESCAPE key, the dialog is hidden.
 * /*from   w ww. j a va2 s .  co  m*/
 * @param dialog
 *            the dialog to bind the action to.
 */
public static void bindEscapeAction(final JDialog dialog) {
    InputMap iMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");

    ActionMap aMap = dialog.getRootPane().getActionMap();
    aMap.put("escape", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
        }
    });
}

From source file:Main.java

public static void installEscapeCloseOperation(final JFrame dialog) {
    Action dispatchClosing = new AbstractAction() {
        public void actionPerformed(ActionEvent event) {
            dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
        }//  w w  w  .  java2  s .c o m
    };
    JRootPane root = dialog.getRootPane();
    root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_KEY_STROKE, ESCAPE_KEY);
    root.getActionMap().put(ESCAPE_KEY, dispatchClosing);
}

From source file:Main.java

/**
 * Sets the hot key for focus.//from   w w  w  .  j ava  2s . c o m
 *
 * @param comp
 *            the comp
 * @param keyStroke
 *            the key stroke
 * @param actionName
 *            the action name
 */
public static void setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName) {
    // get the button's Action map
    final ActionMap amap = comp.getActionMap();
    // add an action to the button's action map
    // and give it a name(it can be any object not just String)
    amap.put(actionName, new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent e) {
            // call your a method that contains your action code
            comp.requestFocus();
        }
    });
    // get the input map for the button
    final InputMap imap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    // add a key stroke associated with an action in the action map(action
    // name).
    // imap.put(KeyStroke.getKeyStroke("F1"),"ActionName");
    // you can do the same for more than one key.
    imap.put(KeyStroke.getKeyStroke(keyStroke), actionName);
}

From source file:EscapeDialog.java

License:asdf

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("about to disappear");
            setVisible(false);/*w  w w . ja v a 2  s  .com*/
        }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
}

From source file:Main.java

void initUI() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    int i = 1;/* ww w . jav  a  2s .c o m*/
    for (GraphicsDevice gd : ge.getScreenDevices()) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createLabel(String.valueOf(i)));
        frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit");
        frame.getRootPane().getActionMap().put("exit", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        frame.setLocation(gd.getDefaultConfiguration().getBounds().getLocation());
        frame.setUndecorated(true);
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
        gd.setFullScreenWindow(frame);
        i++;
    }
}

From source file:Main.java

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  ww w .  java 2  s .  c  o m
        }
    });
}