set JDialog Center Of Screen - Java Swing

Java examples for Swing:JDialog

Description

set JDialog Center Of Screen

Demo Code


//package com.java2s;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
    public static void setCenterOfScreen(JDialog dialog) {
        Dimension size = dialog.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Point position = new Point(screenSize.width / 2 - (size.width / 2),
                screenSize.height / 2 - (size.height / 2));
        dialog.setLocation(position);//from   w w  w  .j  a  va2  s.c  o m
    }

    public static void setCenterOfScreen(JFrame frame) {
        Dimension size = frame.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Point position = new Point(screenSize.width / 2 - (size.width / 2),
                screenSize.height / 2 - (size.height / 2));
        frame.setLocation(position);
    }
}

Related Tutorials