Example usage for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW

List of usage examples for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW

Introduction

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

Prototype

int WHEN_IN_FOCUSED_WINDOW

To view the source code for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW.

Click Source Link

Document

Constant used for registerKeyboardAction that means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component.

Usage

From source file:Main.java

/**
 * Sets the given action so it's invoked if the user hits the escape key.
 * @param dialog The dialog to attach the escape key.
 * @param abortAction The action that is invoked if the escape key is pressed.
 *///from  w w w  .j  a  va 2 s  . c  om
public static void setEscapeWindowAction(JDialog dialog, ActionListener abortAction) {
    if (abortAction != null) {
        ((JComponent) dialog.getContentPane()).registerKeyboardAction(abortAction,
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

From source file:Main.java

/**
 * Adds to the dialog a key listener that makes the dialog invisible.
 * /*from   w w w .ja v a 2 s .c o m*/
 * @param dialog
 */
public static void addEscapeKeyCloseAction(final JDialog dialog) {
    dialog.getRootPane().registerKeyboardAction(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:Main.java

public static void addEscAction(JComponent comp, Action action) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), action);
    comp.getActionMap().put(action, action);
}

From source file:Main.java

public static void addEnterAction(JComponent comp, Action action) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), action);
    comp.getActionMap().put(action, action);
}

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));
        }//from ww  w.j  a va2 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

public static void refleshAction(JComponent com, KeyStroke keyStroke) {
    InputMap im = com.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    Object o = im.get(keyStroke);
    if (o == null) {
        im = com.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        o = im.get(keyStroke);/*ww  w . j av a  2  s. c om*/
    }
    if (o != null) {
        Action a = com.getActionMap().get(o);
        a.setEnabled(a.isEnabled());
    }
}

From source file:Main.java

/**
 * Obtine actiunea inregistrata pentru apasarea de tasta
 * @return actiune inregistrata pentru tasta sau null
 *//*from   ww w  .j ava2s. c o  m*/
public static Action getActionForKeystroke(JComponent component, KeyStroke keyStroke) {
    Action whenFocused = getActionForKeystroke(component, JComponent.WHEN_FOCUSED, keyStroke);
    Action whenAncestorOfFocused = getActionForKeystroke(component,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyStroke);
    Action whenInWindow = getActionForKeystroke(component, JComponent.WHEN_IN_FOCUSED_WINDOW, keyStroke);

    //Ordinea preferata pentru a le returna
    if (whenFocused != null)
        return whenFocused;
    if (whenAncestorOfFocused != null)
        return whenAncestorOfFocused;
    if (whenInWindow != null)
        return whenInWindow;

    //Valoare default
    return null;
}

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 .j a v  a  2s  .c  om*/
    };
    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

void initUI() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    int i = 1;/*from w w w  .  j ava 2s . co  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();/*w w w  .ja va  2 s.  co m*/
        }
    });
}