Positions the child Component in the center of the parent - Java java.awt

Java examples for java.awt:Component

Description

Positions the child Component in the center of the parent

Demo Code

/*//from  w  w w  . j  av a  2 s . c o m
 * @(#) DialogUtilities.java 1.0 3/5/2004
 *
 * Copyright (c) 2004 University of York
 * All rights reserved
 *
 */
//package com.java2s;

public class Main {
    /**
     * Positions the child in the center of the parent
     * @param parent Container
     * @param child Container
     */
    public static void positionInCenter(Component parent, Component child,
            int height, int width) {
        int left, top;
        left = parent.getBounds().x + (parent.getWidth() - width) / 2;
        top = parent.getBounds().y + (parent.getHeight() - height) / 2;
        child.setBounds(left, top, width, height);
    }
}

Related Tutorials