set JDialog Center Of Parent - Java Swing

Java examples for Swing:JDialog

Description

set JDialog Center Of Parent

Demo Code


//package com.java2s;
import java.awt.Dimension;
import java.awt.Point;

import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
    public static void setCenterOfParent(JFrame parent, JDialog dialog) {
        Point parentPosition = parent.getLocation();
        Dimension parentSize = parent.getSize();
        Dimension size = dialog.getSize();
        Point position = new Point(parentPosition.x
                + (parentSize.width / 2 - size.width / 2), parentPosition.y
                + (parentSize.height / 2 - size.height / 2));
        dialog.setLocation(position);/*from w  ww. j av a2 s. com*/
    }

    public static void setCenterOfParent(JDialog parent, JDialog dialog) {
        Point parentPosition = parent.getLocation();
        Dimension parentSize = parent.getSize();
        Dimension size = dialog.getSize();
        Point position = new Point(parentPosition.x
                + (parentSize.width / 2 - size.width / 2), parentPosition.y
                + (parentSize.height / 2 - size.height / 2));
        dialog.setLocation(position);
    }
}

Related Tutorials