Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

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

 
Thread Tools Search this Thread
Old 11-15-2005, 12:42 PM   #1
Default 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); }
}



Tom Parson
  Reply With Quote
Old 11-15-2005, 12:59 PM   #2
VisionSet
 
Posts: n/a
Default Re: How to place a window in the center of the screen ?

"Tom Parson" <> wrote in message
news:4379d7b5$0$21942$...
> 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

--
Mike W




VisionSet
  Reply With Quote
Old 11-15-2005, 01:18 PM   #3
carlos@gkpwdun.com
 
Posts: n/a
Default Re: 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);


carlos@gkpwdun.com
  Reply With Quote
Old 11-15-2005, 01:40 PM   #4
Igor Planinc
 
Posts: n/a
Default Re: 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().


Igor Planinc
  Reply With Quote
Old 11-15-2005, 02:08 PM   #5
Chris Smith
 
Posts: n/a
Default Re: 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).


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


Chris Smith
  Reply With Quote
Old 11-15-2005, 05:39 PM   #6
Roedy Green
 
Posts: n/a
Default Re: How to place a window in the center of the screen ?
On 15 Nov 2005 12:42:30 GMT, (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.


Roedy Green
  Reply With Quote
Old 11-15-2005, 05:47 PM   #7
Carl
 
Posts: n/a
Default Re: 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.


Carl
  Reply With Quote
Old 11-15-2005, 10:20 PM   #8
Hal Rosser
 
Posts: n/a
Default Re: How to place a window in the center of the screen ?

"Tom Parson" <> wrote in message
news:4379d7b5$0$21942$...
> 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.




Hal Rosser
  Reply With Quote
Old 11-16-2005, 09:05 AM   #9
Madguy
 
Posts: n/a
Default Re: 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



Madguy
  Reply With Quote
Old 11-17-2005, 12:13 AM   #10
Alex Molochnikov
 
Posts: n/a
Default Re: 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" <> wrote in message
news:4379d7b5$0$21942$...
> 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); }
> }
>





Alex Molochnikov
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
new window minimizes to taskbar -D- A+ Certification 0 03-03-2007 01:07 AM
New releases: World Trade Center, Jackass Number Two & Wicker Man: Updated complete downloadable R1 DVD DB & info lists Doug MacLean DVD Video 0 10-24-2006 06:04 AM
Hook Up DVD Player to a Flat Screen PC Monitor? weft2 DVD Video 13 02-06-2006 06:14 PM
How can I replace a DVD's wide screen movie with a full screen movie from another DVD? blackhole@aol.com DVD Video 15 10-19-2005 03:53 PM
Panasonic Widscreen 86cm TV Info Needed Screen Size TX-86PW300A. The Other Guy. DVD Video 0 01-10-2005 01:32 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46