fade Out Dialog - Java Swing

Java examples for Swing:JDialog

Description

fade Out Dialog

Demo Code


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dialog.ModalityType;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Main{
    public static void fadeOut(final Dialog win, final boolean close) {
        if (!win.isUndecorated()) {
            if (close) {
                SwingUtils.close(win);/* w w w . java2s .c o  m*/
            }
            return;
        }
        final Timer timer = new Timer(30, null);
        timer.setRepeats(true);
        timer.addActionListener(new ActionListener() {
            private float opacity = 1;

            @Override
            public void actionPerformed(ActionEvent e) {
                opacity -= 0.05f;
                win.setOpacity(Math.max(opacity, 0f));
                if (opacity <= 0) {
                    timer.stop();
                    if (close) {
                        SwingUtils.close(win);
                    } else {
                        win.setVisible(false);
                    }
                }
            }
        });
        win.setOpacity(1);
        timer.start();
    }
    public static void close(Window win) {
        if (win != null) {
            win.setVisible(false);
            win.dispose();
        }
    }
}

Related Tutorials