Java Utililty Methods Window Center

List of utility methods to do Window Center

Description

The list of methods to do Window Center are organized into topic(s).

Method

voidcenterOnParent(final Window child, final boolean absolute)
Center on parent ( absolute true/false (exact center or 25% upper left) )
child.pack();
boolean useChildsOwner = child.getOwner() != null
        ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
        : false;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen()
        : new Point(0, 0);
...
voidcenterPosition(Window window, Window w, int width, int height)
center Position
int x = window.getX() + (window.getWidth() - width) / 2;
int y = window.getY() + (window.getHeight() - height) / 2;
w.setBounds(x, y, width, height);
voidcenterWindow(Window w)
Center a window on the screen.
Dimension screenSize = w.getToolkit().getScreenSize();
Dimension windowSize = w.getSize();
int x = (screenSize.width - windowSize.width) / 2;
int y = (screenSize.height - windowSize.height) / 2;
w.setLocation(x, y);
voidpackAndCenterWindow(Window dlg)
pack And Center Window
dlg.pack();
dlg.pack();
Dimension size = dlg.getSize();
Dimension viewableScreenSize = getViewableScreenSize();
if (size.height > viewableScreenSize.height)
    size.height = viewableScreenSize.height;
if (size.width > viewableScreenSize.width)
    size.width = viewableScreenSize.width;
...
voidpackAndDisplayInCenterOfScreen(final Window f)
Packs a Window and display it in center of the screen.
f.pack();
displayInCenterOfScreen(f);
voidpositionCenterScreen(Window window)
position Center Screen
Dimension maxDimension = Toolkit.getDefaultToolkit().getScreenSize();
Dimension wSize = window.getSize();
int maxWidth = maxDimension.width;
int maxHeight = maxDimension.height;
if (wSize.height > maxHeight) {
    wSize.height = maxHeight;
if (wSize.width > maxWidth) {
...
voidshowCentered(Window window)
Shows the window centered on its parent window, or the screen if there is no parent window.
center(window);
window.setVisible(true);
voidshowCenterWindow(Window parent, Window window)
show Center Window
positionCenterWindow(parent, window);
window.setVisible(true);