Java JComponent Container centerComponent(final JComponent target)

Here you can find the source of centerComponent(final JComponent target)

Description

Centers JComponent on screen

License

Apache License

Parameter

Parameter Description
target a parameter

Declaration

public static void centerComponent(final JComponent target) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;
import java.awt.*;

public class Main {
    /**//from www  . j a  v a2  s  .c  o m
     * Centers JComponent on screen
     *
     * @param target
     */
    public static void centerComponent(final JComponent target) {
        final Rectangle screen = getScreenRect();
        final Rectangle frameSize = target.getBounds();
        final int x = (screen.width - frameSize.width) / 2;
        final int y = (screen.height - frameSize.height) / 2;
        target.setLocation(x, y);
    }

    /**
     * Gers screen rectangle
     *
     * @return
     */
    private static Rectangle getScreenRect() {
        final Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration().getBounds();
        return screen;
    }
}

Related

  1. allignBottom(JComponent lead, JComponent target)
  2. centerOnComponet(Window target, JComponent parent)
  3. checkComponents(final Component _rootComponent, final String _print, final int _currentColumn, final int _currentRow)
  4. findMainRootPane(Component component)
  5. findRootPane(Component component)