Java tutorial
//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 { /** * Creates an animation to fade the dialog opacity from 1 to 0. Using a * default delay of 5 ms and an increment size of 0.05. * * @param dialog the dialog to fade out */ public static void fadeOut(final JDialog dialog) { fadeOut(dialog, 5, 0.05f); } /** * Creates an animation to fade the dialog opacity from 1 to 0, and then * dispose. * * @param dialog the dialog to fade out * @param delay the delay in ms before starting and between each change * @param incrementSize the increment size */ public static void fadeOut(final JDialog dialog, int delay, final float incrementSize) { final Timer timer = new Timer(delay, null); timer.setRepeats(true); timer.addActionListener(new ActionListener() { private float opacity = 1; @Override public void actionPerformed(ActionEvent e) { opacity -= incrementSize; dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7 if (opacity < 0) { timer.stop(); dialog.dispose(); } } }); dialog.setOpacity(1); // requires java 1.7 timer.start(); } }