Java JDialog Escape Key addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose)

Here you can find the source of addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose)

Description

Adds a glass layer to the dialog to intercept all key events.

License

Apache License

Parameter

Parameter Description
dialog the dialog
fadeOnClose the fade on close

Declaration

public static void addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose) 

Method Source Code


//package com.java2s;
/*//  w  ww. ja  va 2s .co m
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.AWTEvent;

import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JComponent;
import javax.swing.JDialog;

import javax.swing.JLayer;

import javax.swing.Timer;
import javax.swing.plaf.LayerUI;

public class Main {
    /**
     * 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).
     *
     * @param dialog
     *            the dialog
     * @param fadeOnClose
     *            the fade on close
     */
    public static void addEscapeToCloseSupport(final JDialog dialog, final boolean fadeOnClose) {
        final LayerUI<Container> layerUI = new LayerUI<Container>() {
            /**
             *
             */
            private static final long serialVersionUID = -7197890707453641705L;
            private boolean closing = false;

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

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

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

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

    /**
     * Creates an animation to fade the dialog opacity from 1 to 0.
     *
     * @param dialog
     *            the dialog
     */
    public static void fadeOut(final JDialog dialog) {
        final Timer timer = new Timer(10, null);
        timer.setRepeats(true);
        timer.addActionListener(new ActionListener() {
            private float opacity = 1;

            @Override
            public void actionPerformed(final ActionEvent e) {
                this.opacity -= 0.15f;
                dialog.setOpacity(Math.max(this.opacity, 0));
                if (this.opacity <= 0) {
                    timer.stop();
                    dialog.dispose();
                }
            }
        });

        dialog.setOpacity(1);
        timer.start();
    }
}

Related

  1. addEscapeListener(final JDialog dialog)
  2. addEscapeListener(final JDialog dialog)
  3. addEscapeListener(final JDialog dialog)
  4. addEscapeListener(final JDialog dialog, final boolean hide)
  5. addEscapeToCloseSupport(final JDialog dialog)
  6. addEscKeyAction(javax.swing.JDialog dialog, javax.swing.Action action)
  7. closeOnEsc(final JDialog dlg)
  8. closeOnEsc(JDialog dialog)
  9. closeOnEscape(final JDialog dialog)