This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes urgent:override a jrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "urgent:override a jrame" Watch "urgent:override a jrame" New topic

urgent:override a jrame

oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
need help in making my application dispaly with the dialog box appearing at the center.The "" contains the dialog i want to appear at the center of th "" class.pls somebody help me out.


code 2:
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

frame.getX() + frame.getWidth() / 2 is the horizontal center of the frame. You then subtract half of the dialog width, so its left and right are both half of the dialog width off the center. In other words, the dialog is centered.

Please make sure you don't pack() your dialog or frame after centering, otherwise the centering size will not be the actual size on screen.

How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

You can also try using dialog.setLocationRelativeTo(Component comp)
If comp is null, the window is displayed centered on the screen, else the window is displayed centered on the comp.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Swing/AWT actually has a built in method for this now...


if you pass null into it it will center the Window/JFrame/JDialog on the screen (based on size). If you call:

it will center the dialog over the center of the frame (the frame has to be visible at the time this is called for it to work).

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Ease Up. We'd appreciate it if you would edit your post and remove Urgent from the subject.

GenRocket - Experts at Building Test Data
I agree. Here's the link:
subject: urgent:override a jrame
It's not a secret anymore!