Example usage for javax.swing.plaf LayerUI LayerUI

List of usage examples for javax.swing.plaf LayerUI LayerUI

Introduction

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

Prototype

LayerUI

Source Link

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  w w. jav a  2  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)./*from  w ww . j a va 2  s .c  om*/
 */
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);
}