Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a JFrame appear on the screen where you want it

Status
Not open for further replies.

mcowen

Programmer
Oct 21, 2001
134
GB
Hi all,

How? All the frames on my program including dialogs are created in the top left corner. How can I make it start up in the center of the screen?

Rgds
Matt
 
Set The Layout of the Frame as Null and then SetBounds to all the components of ythe frame.
 
Sorry Matt,

I clicked on the tab key by accident. Here is the rest of the information you need about the toolkit.

Toolkit tk = Toolkit.getDefaultToolkit(); //Creates an
//instance of the toolkit called tk.

Dimension d = tk.getScreenSize(); //Determines the
//screen size, and puts the value in d.

//once the screen size has been determined by the above
//lines of code it is simply a matter of using setBounds,
//setSize or setLocation.

To put the frame at a particular location simply do this, following on from above.

setLocation(d.width/4,d.height/4);

Of course d.width and d.height can be devided by any whole number, but if this is not good enough simply multiply by a fraction.

e.g.: d.width * 234/1356

The example is pretty extreme but it does demonstrate that any fraction is acceptable.

Happy hunting.

Regards

D
 
I have an open source project called JTUtilities ( and it has a utility methods to handle the placement of your windows. Methods like:
Code:
cascadeRelativeFrom(Component from, Component target)
centerOnScreen(Component target) 
centerRelativeFrom(Component from, Component target)
Plus since it is open source an if you want to learn how these things are done feel free to look through the source that is provided with the library.

There are lots of other useful methods, plus many useful classes, including a budding GUI Componet library.

Please let me know if you find it useful. I am always look for feedback (positive, or negitive) and ways to improve the library.

Good luck...

Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top