From d4c6cfca50b43ce26f937132e046d54830dd6daf Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 5 Apr 2008 17:27:25 +0000 Subject: [PATCH] add method to repack and recenter window git-svn-id: http://trooper/svn/projects/jSite/trunk@610 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/util/swing/SwingUtils.java | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/net/pterodactylus/util/swing/SwingUtils.java b/src/net/pterodactylus/util/swing/SwingUtils.java index b107776..103297b 100644 --- a/src/net/pterodactylus/util/swing/SwingUtils.java +++ b/src/net/pterodactylus/util/swing/SwingUtils.java @@ -20,6 +20,8 @@ package net.pterodactylus.util.swing; import java.awt.Dimension; +import java.awt.Point; +import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; @@ -43,4 +45,29 @@ public class SwingUtils { window.setLocation((screenDimension.width - frameDimension.width) >> 1, (screenDimension.height - frameDimension.height) >> 1); } + /** + * {@link Window#pack() Packs} the given window and positions it so that its + * center stays the same. + * + * @param window + * The window to pack and recenter + */ + public static void repackCentered(Window window) { + Point center = getCenter(window.getBounds()); + window.pack(); + window.setLocation((center.x - window.getWidth() / 2), (center.y - window.getHeight() / 2)); + window.repaint(); + } + + /** + * Returns the center of the given rectangle. + * + * @param bounds + * The rectangle which center to get + * @return The center of the rectangle + */ + private static Point getCenter(Rectangle bounds) { + return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); + } + } -- 2.7.4