Java Window fadeOut(final Window window, final boolean dispose)

Here you can find the source of fadeOut(final Window window, final boolean dispose)

Description

Fade out.

License

Apache License

Parameter

Parameter Description
window the window
dispose the dispose

Declaration

public static void fadeOut(final Window window, final boolean dispose) 

Method Source Code

//package com.java2s;
/*//from   w  w w. j  av  a 2s  . co  m
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main {
    private static final float OPCICITY_INC_DEC = 0.10f;
    private static final int FADE_TIMER_DURATION = 3;

    /**
     * Fade out.
     *
     * @param window
     *            the window
     * @param dispose
     *            the dispose
     */
    public static void fadeOut(final Window window, final boolean dispose) {
        enableParentOfModalDialog(window);
        final Timer timer = new Timer(FADE_TIMER_DURATION, null);
        timer.setRepeats(true);
        timer.addActionListener(new ActionListener() {
            private float opacity = 1;

            @Override
            public void actionPerformed(final ActionEvent e) {
                this.opacity -= OPCICITY_INC_DEC;
                window.setOpacity(Math.max(this.opacity, 0));
                if (this.opacity <= 0) {
                    timer.stop();
                    if (dispose) {
                        window.dispose();
                    } else {
                        window.setVisible(false);
                    }
                }
            }
        });
        window.setOpacity(1);
        timer.start();
    }

    /**
     *
     * @param window
     */
    private static void enableParentOfModalDialog(final Window window) {
        if (window instanceof JDialog && window.getParent() instanceof JFrame) {
            if (((JDialog) window).isModal()) {
                enable((JFrame) window.getParent());
            }
        }
    }

    /**
     * Enable.
     *
     * @param frame
     *            the frame
     */
    public static void enable(final JFrame frame) {
        frame.setGlassPane(new JPanel());
        frame.getGlassPane().setVisible(false);
    }
}

Related

  1. closeByESC(final Window window, JPanel panel)
  2. createWindow(String name, int width, int height, boolean guiOn)
  3. decorate(final Window w)
  4. dock(Window window, Window dockTo)
  5. enableCloseWindowWithEscape(final W window)
  6. flashMessage(final Window parent, String string)
  7. getVisibleWindowByName(String name)
  8. getWindow(final Object source)
  9. getWindow(String title)