Center a Window, Frame, JFrame, Dialog, etc., but do it the American Spelling Way :-) - Java Swing

Java examples for Swing:JDialog

Description

Center a Window, Frame, JFrame, Dialog, etc., but do it the American Spelling Way :-)

Demo Code


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Main{
    /** Center a Window, Frame, JFrame, Dialog, etc., 
     * but do it the American Spelling Way :-)
     *//* w ww.  ja v  a2 s .co m*/
    public static void center(final Window w) {
        UtilGUI.centre(w);
    }
    /** Centre a Window, Frame, JFrame, Dialog, etc. */
    public static void centre(final Window w) {
        // After packing a Frame or Dialog, centre it on the screen.
        Dimension us = w.getSize(), them = Toolkit.getDefaultToolkit()
                .getScreenSize();
        int newX = (them.width - us.width) / 2;
        int newY = (them.height - us.height) / 2;
        w.setLocation(newX, newY);
    }
}

Related Tutorials