How to place a window in the center of the screen ?

T

Tom Parson

I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

How do I do this? My (simplified) current code looks like:



public class testGBL extends JFrame implements ActionListener {
JLabel lab1;
....
JButton butBack;

public testGBL() {

...
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);

c.fill = GridBagConstraints.HORIZONTAL;

butBack = new JButton("Back");
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.gridwidth = 2;
gridbag.setConstraints(butBack, c);
butBack.setEnabled(false);
butBack.addActionListener(this);
contentPane.add(butBack);
....
}

public static void main(String args[]) {
testGBL window = new testGBL();
window.setTitle("test");
window.pack();
window.setVisible(true); }
}
 
V

VisionSet

Tom Parson said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

window.setLocation(int x, int y);

or if you want it in the centre use this cludge:

window.setLocationRelativeTo(null);

should take a component as the argument and centre it on that
 
C

carlos

I would like to open a window and to placed it at a certain
position on a screen (e.g. in the center).

How do I do this? My (simplified) current code looks like:

public class testGBL extends JFrame implements ActionListener {
JLabel lab1;
....
JButton butBack;

public testGBL() {

...
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);

c.fill = GridBagConstraints.HORIZONTAL;

butBack = new JButton("Back");
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.gridwidth = 2;
gridbag.setConstraints(butBack, c);
butBack.setEnabled(false);
butBack.addActionListener(this);
contentPane.add(butBack);
....
}

public static void main(String args[]) {
testGBL window = new testGBL();
window.setTitle("test");
window.pack();
window.setVisible(true); }
}


Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally.
int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically.

window.setBounds(X,Y , widthWindow,heightWindow);
 
I

Igor Planinc

Tom said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

How do I do this? My (simplified) current code looks like:



public class testGBL extends JFrame implements ActionListener {
JLabel lab1;
....
JButton butBack;

public testGBL() {

...
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);

c.fill = GridBagConstraints.HORIZONTAL;

butBack = new JButton("Back");
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.gridwidth = 2;
gridbag.setConstraints(butBack, c);
butBack.setEnabled(false);
butBack.addActionListener(this);
contentPane.add(butBack);
....
}

public static void main(String args[]) {
testGBL window = new testGBL();
window.setTitle("test");
window.pack();
window.setVisible(true); }
}

..setLocation() or .setBounds().
 
C

Chris Smith

Tom Parson said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

There's an easy way to center a Swing JFrame on the screen:

f.setLocationRelativeTo(null);

Do this after the pack() or setSize(...) call.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Carl

Tom said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

How do I do this? My (simplified) current code looks like:



public class testGBL extends JFrame implements ActionListener {
....snip

Tom,

You can call the the setBounds method to fine tune the components
location and size on the screen.

A java.awt.Toolkit object will also be helpful if you want information
son the screen size in order to place your component in a relative location.

Carl.
 
H

Hal Rosser

Tom Parson said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

How do I do this? My (simplified) current code looks like:



public class testGBL extends JFrame implements ActionListener {
JLabel lab1;
....
JButton butBack;

public testGBL() {

...
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);

c.fill = GridBagConstraints.HORIZONTAL;
Take a look at the Toolkit (getDefaultToolkit) and its getScreenSize()
method which returns a DImension object, which has width and height
properties of the screen.
Then you use the setBounds method of the JFrame to set the x,y,width,and
height from your meticulous calculations.
 
M

Madguy

after packing the window, you must set the position of the window like
this.

left_of_window = (screen_resolution_width - width_of_the_window)/2
top_of_the_window = (screen_resolution_height - height_of_the_window)
/2

this would center the window in the screen.

/Cheers,
-MadGuy
 
A

Alex Molochnikov

To place the JFrame at the desired origin, use setLocation() method. To
center JFrame on the screen, use setLocationRelativeTo(null).

Reading the JFrame docs could also help.
 
R

red eff

(e-mail address removed) (Tom Parson) wrote in
I would like to open a window and to placed it at a certain position
on a screen (e.g. in the center).

public static void main(String args[]) {
testGBL window = new testGBL();
window.setTitle("test");
window.pack();
window.setVisible(true); }
}

put this in ---

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((d.getWidth() - this.getWidth())/ 2);
int y = (int) ((d.getHeight() - this.getHeight())/ 2);
this.setLocation(x, y);
//this.setAlwaysOnTop(true); // show on top - 5.0
this.setVisible(true);
 
J

jonck

I would like to open a window and to placed it at a certain position on a screen

If the variables "width" and "height" are the width and height of your
JFrame, something like this:

int width = 700;
int height = 233;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height - height)/2;
setBounds(x,y,width,height);
 
T

tele2

I had to do this a few days ago.
This is the code I wrote :

public static void center(JFrame frame) {

frame.pack();
double height = frame.getHeight();
double width = frame.getWidth();

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

double x = screen.getWidth() - width;
double y = screen.getHeight() - height;
x = x / 2.0;
y = y / 2.0;

if ( x < 0) x = 0;
if ( y < 0) y = 0;

frame.setBounds((int)x,(int)y,(int)width,(int)height);




}

Hope this will help

Olivier
 
B

Brandon McCombs

Tom said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).

How do I do this? My (simplified) current code looks like:

This puts the window itself in the middle of the screen and not just the
top left corner of the window.

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width/4, dim.height/4);
frame.setLocation(dim.width/2 - dim.width/8, dim.height/2 - dim.height/8);
public class testGBL extends JFrame implements ActionListener {
JLabel lab1;
....
JButton butBack;

public testGBL() {

...
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);

c.fill = GridBagConstraints.HORIZONTAL;

butBack = new JButton("Back");
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.gridwidth = 2;
gridbag.setConstraints(butBack, c);
butBack.setEnabled(false);
butBack.addActionListener(this);
contentPane.add(butBack);
....
}

public static void main(String args[]) {
testGBL window = new testGBL();
window.setTitle("test");
window.pack();
window.setVisible(true); }
}
 
R

Raja

Here is the code to place ur frame in the center of the screen:

Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();

if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}

if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}

int frameX = (screenSize.height - frameSize.height) / 2;
int frameY = (screenSize.height - frameSize.height) / 2;

frame.setLocation(frameX, frameY);
 
V

Vova Reznik

Raja said:
Here is the code to place ur frame in the center of the screen:

Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();

if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}

if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}

int frameX = (screenSize.height - frameSize.height) / 2;
width????

int frameY = (screenSize.height - frameSize.height) / 2;

frame.setLocation(frameX, frameY);
 
K

Kent Paul Dolan

Tom said:
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center).
How do I do this?

Specifically to place it in the center (but not some arbitrary other
place),
Sun shows a pretty way to do this in:

http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/FrameDemo2.java

The line in question says (for a JFrame):

frame.setLocationRelativeTo(null); //center it

and is line 263 in that example. That actually works, though why
it does is probably some Java internals defaults mystery.

Otherwise, the usual trick is to use setLocation, which uses the frame
top left corner coordinates (in some form) as its input.

HTH

xanthian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top