Java Utililty Methods Screen Center

List of utility methods to do Screen Center

Description

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

Method

voidcentreOnScreen(Window frame)
Centres a window to screen
frame.setLocationRelativeTo(null);
voidcentreOnScreen(Window win)
Centre the given window on the screen.
Dimension screenRes = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = win.getSize();
int x = Math.max(0, (screenRes.width - windowSize.width) / 2);
int y = Math.max(0, (screenRes.height - windowSize.height) / 2);
win.setLocation(x, y);
PointgetCenter(Component owner, Dimension size)
get Center
Dimension d = owner.getSize();
Point result = new Point((d.width / 2 - size.width / 2) + owner.getLocation().x,
        (d.height / 2 - size.height / 2) + owner.getLocation().y);
return result;
PointgetCenterCoordinates(Dimension d)
get Center Coordinates
Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
Point ul = new Point();
ul.x = (s.width - d.width) / 2;
ul.y = (s.height - d.height) / 2;
return (ul);
int[]getCenteredLocation(Component comp)
get Centered Location
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int top = (screenSize.height - comp.getHeight()) / 2;
int left = (screenSize.width - comp.getWidth()) / 2;
return new int[] { left, top };
PointgetCenteredLocation(Window w)
get Centered Location
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = d.width - (d.width / 2 + w.getWidth() / 2);
int y = d.height - (d.height / 2 + w.getHeight() / 2);
Point p = new Point(x, y);
return p;
PointgetCenteredLocation(Window window)
Calculates the location of a window so that it will be centered on the screen.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = window.getSize();
if (windowSize.width > screenSize.width) {
    windowSize.width = screenSize.width;
if (windowSize.height > screenSize.height) {
    windowSize.height = screenSize.height;
return new Point((screenSize.width - windowSize.width) / 2, (screenSize.height - windowSize.height) / 2);
PointgetCenteringPoint(Dimension size)
Get the point on point on the screen at which to open a dialog or window for it to appear centered.
Point centeringPoint = new Point();
Dimension screenSize;
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (size.height > screenSize.height) {
    size.height = screenSize.height;
if (size.width > screenSize.width) {
    size.width = screenSize.width;
...
PointgetCenteringPoint(final Dimension frameDimension)
Returns the centered point in order to center a frame on the screen
getScreenProperties();
int x = (_screenWidth - frameDimension.width) / 2;
x = Math.max(x, 0);
int y = (_screenHeight - frameDimension.height) / 2;
y = Math.max(y, 0);
return new Point(x, y);
PointgetCenteringPointOnScreen(Dimension dimension)
Return the centering point on the screen for the object with the specified dimension.
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
if (dimension.width > screen.width) {
    dimension.width = screen.width;
if (dimension.height > screen.height) {
    dimension.height = screen.height;
return new Point((screen.width - dimension.width) / 2, (screen.height - dimension.height) / 2);
...