Java JDialog Fade fadeIn(final JDialog dialog)

Here you can find the source of fadeIn(final JDialog dialog)

Description

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

License

Open Source License

Declaration

public static void fadeIn(final JDialog dialog) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import javax.swing.JDialog;

import javax.swing.Timer;

public class Main {
    /**//from   w w w .  j av  a2 s  . c  o  m
     * 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

  1. fadeIn(final JDialog dialog, int delay, final float incrementSize)
  2. fadeInAndOut(final JDialog dialog)