JavaRanch » Java Forums »
Java »
Swing / AWT / SWT
| Author |
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 "InputDialog.java" contains the dialog i want to appear at the center of th "FoodFrame.java" class.pls somebody help me out. codes: //InputDialog.java code 2: //FoodFrame.java
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8435
|
|
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.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Nathan Pruett
Bartender
Joined: Oct 18, 2000
Posts: 4121
|
|
Swing/AWT actually has a built in method for this now... Window.setLocationRelativeTo(Component) 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).
|
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
|
Ease Up. We'd appreciate it if you would edit your post and remove Urgent from the subject.
|
 |
 |
|
|
subject: urgent:override a jrame
|
|
|
|