List of usage examples for java.awt.event KeyEvent VK_ESCAPE
int VK_ESCAPE
To view the source code for java.awt.event KeyEvent VK_ESCAPE.
Click Source Link
From source file:Main.java
public static void main(String[] args) { JDialog dialog;/* www.j a va 2 s . co m*/ JList jlist; ActionListener otherListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("current"); } }; JButton okButton = new JButton("OK"); okButton.addActionListener(e -> close(true)); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(e -> close(false)); jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" }); jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jlist.setVisibleRowCount(5); JScrollPane scroll = new JScrollPane(jlist); JPanel buttonsPanel = new JPanel(new FlowLayout()); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); JPanel content = new JPanel(new BorderLayout()); content.add(scroll, BorderLayout.CENTER); content.add(buttonsPanel, BorderLayout.SOUTH); dialog = new JDialog((Frame) null, true); dialog.setContentPane(content); dialog.pack(); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething"); dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); dialog.setVisible(true); }
From source file:Main.java
public static void addEscapeExitListeners(final JFrame window) { addKeyAdapterRecursively(window, new KeyAdapter() { @Override/* w w w .j a v a 2 s . co m*/ public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { System.exit(0); } } }); }
From source file:Main.java
/** * Adds a key listener to a given {@link JDialog} that diposes it when the escape * key is pressed./*from w ww . j a v a 2 s .c om*/ */ public static void addEscapeKeyCloseAction(final JDialog dialog) { dialog.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.dispose(); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); }
From source file:Main.java
public static void closeOnEscape(final JDialog parent) { parent.getRootPane().getActionMap().put("close_on_escape", new AbstractAction() { @Override//from w w w.ja v a 2 s . com 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
/** * 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 av a 2s. c o m 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
/** * Attaches a key event listener to given component, disposing of the given window * upon pressing escape within the context. * /*from ww w. j a va 2 s .c o m*/ * @param context * @param button */ public static void simulateExitOnEscape(Component context, JFrame window) { context.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { for (WindowListener wl : window.getWindowListeners()) { wl.windowClosing(new WindowEvent(window, WindowEvent.WINDOW_CLOSING)); } if (window != null) window.dispose(); } } }); }
From source file:Main.java
/** * Binds an action to the dialog so when the user presses the ESCAPE key, the dialog is hidden. * /* w w w. j a va2 s . c o 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
private static boolean isNonEditKey(int keyCode) { switch (keyCode) { case KeyEvent.VK_UP: case KeyEvent.VK_DOWN: case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: case KeyEvent.VK_CAPS_LOCK: case KeyEvent.VK_CONTROL: case KeyEvent.VK_ALT: case KeyEvent.VK_SHIFT: case KeyEvent.VK_INSERT: case KeyEvent.VK_HOME: case KeyEvent.VK_END: case KeyEvent.VK_PAGE_DOWN: case KeyEvent.VK_PAGE_UP: case KeyEvent.VK_PRINTSCREEN: case KeyEvent.VK_NUM_LOCK: case KeyEvent.VK_ESCAPE: return true; }/*from w w w. j a v a 2 s . c o m*/ if (keyCode >= KeyEvent.VK_F1 && keyCode <= KeyEvent.VK_F12) { return true; } return false; }
From source file:Main.java
/** * Adds to the dialog a key listener that makes the dialog invisible. * //from www .j a va2 s. c om * @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 addEscapeExitListeners(final JDialog window) { addKeyAdapterRecursively(window, new KeyAdapter() { @Override// www . ja v a 2 s. co m public void keyPressed(final KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { System.exit(0); } } }); }