Centers the dialog over the given parent component. - Java Swing

Java examples for Swing:JDialog

Description

Centers the dialog over the given parent component.

Demo Code


import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.plaf.LayerUI;

public class Main{
    /**/*from ww  w .  ja  v  a 2s.c  o m*/
     * Centers the dialog over the given parent component. Also, creates a
     * semi-transparent panel behind the dialog to mask the parent content.
     * The title of the dialog is displayed in a custom fashion over the dialog
     * panel, and a rectangular shadow is placed behind the dialog.
     */
    public static void createDialogBackPanel(JDialog dialog,
            Component parent, boolean addEscapeToCloseSupport) {
        DialogBackPanel newContentPane = new DialogBackPanel(dialog,
                addEscapeToCloseSupport);
        dialog.setContentPane(newContentPane);
        dialog.setSize(parent.getSize());
        dialog.setLocation(parent.getLocationOnScreen());
        if (addEscapeToCloseSupport)
            addEscapeToCloseSupport(dialog);
    }
    /**
     * 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).
     */
    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);
    }
    /**
     * Creates an animation to fade the dialog opacity from 1 to 0.
     */
    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(ActionEvent e) {
                opacity -= 0.15f;
                dialog.setOpacity(Math.max(opacity, 0));
                if (opacity <= 0) {
                    timer.stop();
                    dialog.dispose();
                }
            }
        });

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

Related Tutorials