Click to See Complete Forum and Search --> : Center Dialogs in Screen


Evert Helms
May 2nd, 2000, 02:26 AM
How can I center a Dialog in the middle of the screen or its parent window and how do i get a frames parent window?

Thanks...

dogbear
May 2nd, 2000, 04:57 AM
Evert,

First, you need the screen size.

For Applets:
in the init() method:
OR
For Applications:
in the constructor:
Toolkit toolkit = this.getToolkit();
Dimension d = toolkit.getScreenSize();


At this point d.width and d.height contain your screen width and height, respectively.
You should save these two values to global variables, such as:
Dimension screen_size;
screen_size.width = d.width;
screen_size.height = d.height;


Second, when you show your dialog(or when you instanciate it), insert the following:
MyDialog myDialog = new MyDialog("My Dialog");
// give the dialog a width and height(200 by 100)
// don't worry about the 0,0 location yet
myDialog.setBounds(0,0,200,100);
// now you simply need to find the half-way point
// of the screen and substract half the width(or height)
// of the dialog...
int x = (screen_size.width/2)-100;
int y = (screen_size.height/2)-50;
// reset the dialog's screen position...
myDialog.setLocation(x,y);


And that should do it.
The process is pretty much the same if you're using Swing.
Hope this helps.

Regards,
dogBear

bharath
May 3rd, 2000, 12:07 AM
hai,

U Can use

public Dimension size=java.awt.ToolKit.getScreenSize();

size will return the dimention of the screen so from the given dimensions u can center the screen.

If u want to place at the center of the parent window or if u want to get the parent window u can use

Object b=this.getParent() will return the parent of the current Object.

if the parent is frame
consvert the object into frame using
Frame f=(Frame)b; // object converted before

and the parent obtained u can use the centering the screen.

Or the other way pass the size in the constructor to its child class which being involked.

All the best

-bharath
















S.V Bharath Reddy
Software Engineer,
Hyderabad.