Example usage for java.awt.event KeyEvent VK_ESCAPE

List of usage examples for java.awt.event KeyEvent VK_ESCAPE

Introduction

In this page you can find the example usage for java.awt.event KeyEvent VK_ESCAPE.

Prototype

int VK_ESCAPE

To view the source code for java.awt.event KeyEvent VK_ESCAPE.

Click Source Link

Document

Constant for the ESCAPE virtual key.

Usage

From source file:Main.java

/**
 * Adds a glass layer to the dialog to intercept all key events. If the
 * espace key is pressed, the dialog is disposed (either with a fadeout
 * animation, or directly)./*from w ww . ja  v a2  s .  c  om*/
 */
public static void addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose) {
    LayerUI<Container> layerUI = new LayerUI<Container>() {
        private boolean closing = false;

        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            ((JLayer) c).setLayerEventMask(AWTEvent.KEY_EVENT_MASK);
        }

        @Override
        public void uninstallUI(JComponent c) {
            super.uninstallUI(c);
            ((JLayer) c).setLayerEventMask(0);
        }

        @Override
        public void eventDispatched(AWTEvent e, JLayer<? extends Container> l) {
            if (e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == KeyEvent.VK_ESCAPE) {
                if (closing)
                    return;
                closing = true;
                if (fadeOnClose)
                    fadeOut(dialog);
                else
                    dialog.dispose();
            }
        }
    };

    JLayer<Container> layer = new JLayer<>(dialog.getContentPane(), layerUI);
    dialog.setContentPane(layer);
}

From source file:Main.java

/**
 * Adds a glass layer to the dialog to intercept all key events. If the
 * espace key is pressed, the dialog is disposed (either with a fadeout
 * animation, or directly).//  www  .  j a  v a 2  s. com
 */
public static void addEscapeToCloseSupport(final JDialog dialog) {
    LayerUI<Container> layerUI = new LayerUI<Container>() {

        private boolean closing = false;

        @Override
        public void installUI(JComponent c) {
            super.installUI(c);
            ((JLayer) c).setLayerEventMask(AWTEvent.KEY_EVENT_MASK);
        }

        @Override
        public void uninstallUI(JComponent c) {
            super.uninstallUI(c);
            ((JLayer) c).setLayerEventMask(0);
        }

        @Override
        public void eventDispatched(AWTEvent e, JLayer<? extends Container> l) {
            if (e instanceof KeyEvent && ((KeyEvent) e).getKeyCode() == KeyEvent.VK_ESCAPE) {
                if (closing) {
                    return;
                }
                closing = true;
                fadeOut(dialog);
            }
        }
    };

    JLayer<Container> layer = new JLayer<Container>(dialog.getContentPane(), layerUI);
    dialog.setContentPane(layer);
}

From source file:Main.java

void initUI() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    int i = 1;//from   w  w w. j  a  va 2  s. 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();/*w ww  .  j  av  a2  s  . c  o m*/
        }
    });
}

From source file:EscapeDialog.java

protected JRootPane createRootPane() {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);/* ww w  .j a va2s .  c om*/
        }
    };
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    return rootPane;
}

From source file:TDialog.java

protected JRootPane createRootPane() {
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            hide();//from  w  w  w .java 2 s.  com
            bCancel = true;
        }
    };

    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    JRootPane rootPane = super.createRootPane();

    rootPane.registerKeyboardAction(al, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

    return rootPane;
}

From source file:com.mirth.connect.manager.HeapSizeDialog.java

public HeapSizeDialog(String heapSize) {
    super(PlatformUI.MANAGER_DIALOG, true);

    managerController = ManagerController.getInstance();
    getRootPane().registerKeyboardAction(new ActionListener() {
        @Override//from   w  w w .j  a v  a2 s .  co  m
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

    setResizable(false);
    setBackground(Color.white);
    setTitle("Web Start Settings");
    getContentPane().setBackground(Color.white);

    initComponents();

    this.heapSize = StringUtils.isEmpty(heapSize) ? "512m" : heapSize;

    String heapSizeOption = HeapSize.toDisplayName(heapSize);
    if (StringUtils.isBlank(heapSizeOption)) {
        heapSizeOption = this.heapSize;
    }

    // Add any non-default properties to the model
    String property = (String) managerController.getServerProperties()
            .getProperty(ManagerConstants.ADMINISTRATOR_MAX_HEAP_SIZE);
    if (!heapSizeComboboxModel.contains(property)
            && !heapSizeComboboxModel.contains(HeapSize.toDisplayName(property))) {
        heapSizeComboboxModel.add(formatCustomProperty(property));
    }

    // Resort list by sizes
    List<String> mbList = new ArrayList<String>();
    List<String> gbList = new ArrayList<String>();
    for (String size : heapSizeComboboxModel) {
        if (size.contains("M")) {
            mbList.add(size);
        } else {
            gbList.add(size);
        }
    }

    Collections.sort(mbList);
    Collections.sort(gbList);
    mbList.addAll(gbList);

    heapSizeComboBox = new JComboBox(mbList.toArray());
    heapSizeComboBox.getModel().setSelectedItem(formatCustomProperty(heapSizeOption));

    initLayout();
    pack();
    setLocationRelativeTo(PlatformUI.MANAGER_DIALOG);
    setVisible(true);
}

From source file:com.github.alexfalappa.nbspringboot.codegen.SpringDependencyDialog.java

/** Creates new form NewOkCancelDialog */
public SpringDependencyDialog(java.awt.Frame parent) {
    super(parent, true);
    initComponents();//  www . j  av  a 2  s .c  om
    // Close the dialog when Esc is pressed
    String cancelName = "cancel";
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
    ActionMap actionMap = rootPane.getActionMap();
    actionMap.put(cancelName, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doClose(RET_CANCEL);
        }
    });
}

From source file:com.konifar.material_icon_generator.FilterComboBox.java

private void initListener() {
    final JTextField textfield = (JTextField) this.getEditor().getEditorComponent();
    textfield.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent event) {
            switch (event.getKeyCode()) {
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_ESCAPE:
                requestFocus(false);//from w  w  w  . j  ava2s. co m
                break;
            case KeyEvent.VK_UP:
            case KeyEvent.VK_DOWN:
                break;
            default:
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        filter(textfield.getText());
                    }
                });
            }
        }
    });

    getAccessibleContext().addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent event) {
            if (AccessibleContext.ACCESSIBLE_STATE_PROPERTY.equals(event.getPropertyName())
                    && AccessibleState.FOCUSED.equals(event.getNewValue())
                    && getAccessibleContext().getAccessibleChild(0) instanceof ComboPopup) {
                ComboPopup popup = (ComboPopup) getAccessibleContext().getAccessibleChild(0);
                JList list = popup.getList();

                if (list.getSelectedValue() != null) {
                    setSelectedItem(String.valueOf(list.getSelectedValue()));
                }
            }
        }
    });
}

From source file:com.mirth.connect.client.ui.components.KeyStrokeTextField.java

@Override
public void keyTyped(KeyEvent evt) {
    if (keyCode != null && (evt.getKeyCode() == KeyEvent.VK_ENTER || evt.getKeyCode() == KeyEvent.VK_ESCAPE)) {
        return;/*from   ww w  .j a  v  a2 s .com*/
    }

    // Clear the text field if backspace is pressed
    if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE || evt.getKeyChar() == '\b') {
        reset();
        PlatformUI.MIRTH_FRAME.setSaveEnabled(true);
    }
    evt.consume();
}