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, 11:42 AM   #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, 11:59 AM   #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


  Reply With Quote
Old 11-15-2005, 12: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);
  Reply With Quote
Old 11-15-2005, 12: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().
  Reply With Quote
Old 11-15-2005, 01: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
  Reply With Quote
Old 11-15-2005, 04: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.
  Reply With Quote
Old 11-15-2005, 04: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.
  Reply With Quote
Old 11-15-2005, 09: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.


  Reply With Quote
Old 11-16-2005, 08: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

  Reply With Quote
Old 11-16-2005, 11:13 PM   #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); }
> }
>



  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
Forum Jump