Java Window decorate(final Window w)

Here you can find the source of decorate(final Window w)

Description

decorate

License

Open Source License

Declaration

public static void decorate(final Window w) 

Method Source Code


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

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.KeyStroke;

public class Main {
    private final static List<Image> ICONS = new ArrayList<Image>();

    public static void decorate(final Window w) {
        center(w);//from w w w .ja v a2 s  . com
        setIcon(w);
    }

    public static void decorate(final JDialog d, boolean closeOnEscape) {
        center(d);
        setIcon(d);
        if (closeOnEscape) {
            addEscapeListener(d);
        }
    }

    private static void center(Window w) {
        Dimension windowSize = w.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - windowSize.width) / 2;
        int y = (screenSize.height - windowSize.height) / 2;
        w.setLocation(x, y);
    }

    private static void setIcon(Window w) {
        w.setIconImages(ICONS);
    }

    public static void addEscapeListener(final JDialog d) {
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                d.setVisible(false);
            }
        };
        d.getRootPane().registerKeyboardAction(al, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}

Related

  1. arrangeWithin(final Shape shapeToArrange, final Rectangle window, final int arrangement, Insets padding)
  2. buildWindow(Float opacity)
  3. cascade(Window[] windows, Rectangle dBounds, int separation)
  4. closeByESC(final Window window, JPanel panel)
  5. createWindow(String name, int width, int height, boolean guiOn)
  6. dock(Window window, Window dockTo)
  7. enableCloseWindowWithEscape(final W window)
  8. fadeOut(final Window window, final boolean dispose)
  9. flashMessage(final Window parent, String string)