Java Window flashMessage(final Window parent, String string)

Here you can find the source of flashMessage(final Window parent, String string)

Description

flash Message

License

Open Source License

Declaration

public static void flashMessage(final Window parent, String string) 

Method Source Code

//package com.java2s;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;

import java.awt.Point;
import java.awt.Window;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public class Main {
    public static void flashMessage(final Window parent, String string) {
        flashMessage(parent, string, Color.WHITE, Color.BLACK, 1000);
    }/*from  w  w  w  .ja v a  2  s  . co m*/

    public static void flashMessage(final Window parent, String string,
            Color background, Color foreground, final long howLong) {
        final int fontSize = 20;
        final JLabel label = new JLabel(string);
        final JDialog dialog = new JDialog(parent);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
        label.setFont(label.getFont().deriveFont(fontSize));
        label.setBackground(background);
        label.setForeground(foreground);
        label.setBorder(new BevelBorder(BevelBorder.RAISED));
        dialog.getRootPane().setLayout(new BorderLayout());
        dialog.getRootPane().add(label);
        final Point point = parent.getLocation();
        dialog.setLocation(point.x + parent.getHeight() / 2, point.y
                + parent.getWidth() / 2);
        dialog.setSize(fontSize * string.length() * 6 / 10, fontSize);
        dialog.setUndecorated(true);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
        parent.requestFocus();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(howLong);
                } catch (InterruptedException e) {
                }
                dialog.dispose();
            }
        });
    }
}

Related

  1. createWindow(String name, int width, int height, boolean guiOn)
  2. decorate(final Window w)
  3. dock(Window window, Window dockTo)
  4. enableCloseWindowWithEscape(final W window)
  5. fadeOut(final Window window, final boolean dispose)
  6. getVisibleWindowByName(String name)
  7. getWindow(final Object source)
  8. getWindow(String title)
  9. getWindowNormalBounds(Window window)