How to place a window in the center of the screen ?  
Author Message
tom.parson





PostPosted: 2005-11-15 20:43:00 Top

java-programmer, How to place a window in the center of the screen ? 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); }
}

 
VisionSet





PostPosted: 2005-11-15 21:00:00 Top

java-programmer >> How to place a window in the center of the screen ?
"Tom Parson" <email***@***.com> wrote in message
news:4379d7b5$0$21942$email***@***.com...
> 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


 
carlos





PostPosted: 2005-11-15 21:18:00 Top

java-programmer >> How to place a window in the center of the screen ?
>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);
 
 
Igor Planinc





PostPosted: 2005-11-15 21:40:00 Top

java-programmer >> How to place a window in the center of the screen ? Tom Parson wrote:
> 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().
 
 
Chris Smith





PostPosted: 2005-11-15 22:08:00 Top

java-programmer >> How to place a window in the center of the screen ? Tom Parson <email***@***.com> wrote:
> 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
 
 
Roedy Green





PostPosted: 2005-11-16 1:39:00 Top

java-programmer >> How to place a window in the center of the screen ? On 15 Nov 2005 12:42:30 GMT, email***@***.com (Tom Parson) wrote,
quoted or indirectly quoted someone who 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:

see http://mindprod.com/jgloss/coordinates.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Carl





PostPosted: 2005-11-16 1:48:00 Top

java-programmer >> How to place a window in the center of the screen ? Tom Parson wrote:
> 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.
 
 
Hal Rosser





PostPosted: 2005-11-16 6:21:00 Top

java-programmer >> How to place a window in the center of the screen ?
"Tom Parson" <email***@***.com> wrote in message
news:4379d7b5$0$21942$email***@***.com...
> 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.


 
 
Madguy





PostPosted: 2005-11-16 17:05:00 Top

java-programmer >> How to place a window in the center of the screen ? 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

 
 
Alex Molochnikov





PostPosted: 2005-11-17 8:14:00 Top

java-programmer >> How to place a window in the center of the screen ? 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.

"Tom Parson" <email***@***.com> wrote in message
news:4379d7b5$0$21942$email***@***.com...
> 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); }
> }
>


 
 
red eff





PostPosted: 2005-11-17 9:53:00 Top

java-programmer >> How to place a window in the center of the screen ? email***@***.com (Tom Parson) wrote in
news:4379d7b5$0$21942$email***@***.com:

> 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);

 
 
jonck





PostPosted: 2005-11-17 21:13:00 Top

java-programmer >> How to place a window in the center of the screen ? > 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);

 
 
tele2





PostPosted: 2005-11-20 6:32:00 Top

java-programmer >> How to place a window in the center of the screen ? 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

"Tom Parson" <email***@***.com> a 閏rit dans le message de news:
4379d7b5$0$21942$email***@***.com...
>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); }
> }
>


 
 
Brandon McCombs





PostPosted: 2005-11-21 5:48:00 Top

java-programmer >> How to place a window in the center of the screen ? Tom Parson wrote:
> 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); }
> }
>
 
 
Raja





PostPosted: 2005-11-24 5:35:00 Top

java-programmer >> How to place a window in the center of the screen ? 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);

 
 
Vova Reznik





PostPosted: 2005-11-24 5:36:00 Top

java-programmer >> How to place a window in the center of the screen ? Raja wrote:
> 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);
>

 
 
Kent Paul Dolan





PostPosted: 2005-11-24 8:59:00 Top

java-programmer >> How to place a window in the center of the screen ? Tom Parson wrote:

> 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.