move Component To Screen Center - Java Swing

Java examples for Swing:Screen

Description

move Component To Screen Center

Demo Code


//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;

public class Main {

    public static void moveCompToScreenCenter(Component comp) {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) (dim.getWidth() - comp.getWidth()) / 2;
        int y = (int) (dim.getHeight() - comp.getHeight()) / 2;
        comp.setLocation(x, y);/*from   w w w . ja v a  2  s.com*/
    }
}

Related Tutorials