| Author |
Message |
tom.parson

|
Posted: 11/15/2005 8:43:00 PM |
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

|
Posted: 11/15/2005 9:00:00 PM |
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

|
Posted: 11/15/2005 9:18:00 PM |
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

|
Posted: 11/15/2005 9:40:00 PM |
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

|
Posted: 11/15/2005 10:08:00 PM |
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

|
Posted: 11/16/2005 1:39:00 AM |
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

|
Posted: 11/16/2005 1:48:00 AM |
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

|
Posted: 11/16/2005 6:21:00 AM |
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

|
Posted: 11/16/2005 5:05:00 PM |
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

|
Posted: 11/17/2005 8:14:00 AM |
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

|
Posted: 11/17/2005 9:53:00 AM |
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

|
Posted: 11/17/2005 9:13:00 PM |
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

|
Posted: 11/20/2005 6:32:00 AM |
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

|
Posted: 11/21/2005 5:48:00 AM |
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

|
Posted: 11/24/2005 5:35:00 AM |
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

|
Posted: 11/24/2005 5:36:00 AM |
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

|
Posted: 11/24/2005 8:59:00 AM |
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.
|
| |
|
| |
 |
| |