Creates an animation to fade the dialog opacity from 0 to 1. - Java Swing

Java examples for Swing:JOptionPane

Description

Creates an animation to fade the dialog opacity from 0 to 1.

Demo Code


//package com.java2s;

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

import javax.swing.JDialog;

import javax.swing.Timer;

public class Main {
    /**// ww w.  j a v a  2s .  com
     * Creates an animation to fade the dialog opacity from 0 to 1.
     */
    public static void fadeIn(final JDialog dialog) {
        final Timer timer = new Timer(10, null);
        timer.setRepeats(true);
        timer.addActionListener(new ActionListener() {
            private float opacity = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                opacity += 0.15f;
                dialog.setOpacity(Math.min(opacity, 1));
                if (opacity >= 1)
                    timer.stop();
            }
        });

        dialog.setOpacity(0);
        timer.start();
        dialog.setVisible(true);
    }
}

Related Tutorials