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-17-2005, 01:52 AM   #11
Default Re: How to place a window in the center of the screen ?


(Tom Parson) wrote in
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).
>
> 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);



red eff
  Reply With Quote
Old 11-17-2005, 01:12 PM   #12
jonck@vanderkogel.net
 
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

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



jonck@vanderkogel.net
  Reply With Quote
Old 11-19-2005, 10:32 PM   #13
tele2
 
Posts: n/a
Default Re: 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)heig ht);




}

Hope this will help

Olivier

"Tom Parson" <> a écrit dans le message de 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); }
> }
>





tele2
  Reply With Quote
Old 11-20-2005, 09:47 PM   #14
Brandon McCombs
 
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:
>
>


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

>
> 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
  Reply With Quote
Old 11-23-2005, 09:34 PM   #15
Raja
 
Posts: n/a
Default Re: 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);



Raja
  Reply With Quote
Old 11-23-2005, 09:36 PM   #16
Vova Reznik
 
Posts: n/a
Default Re: 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);
>




Vova Reznik
  Reply With Quote
Old 11-24-2005, 12:58 AM   #17
Kent Paul Dolan
 
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?


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/tutor...rameDemo2.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.



Kent Paul Dolan
  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