File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "urgent:override a jrame" Watch "urgent:override a jrame" New topic
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: 19651
    
  18


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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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: http://aspose.com/file-tools
 
subject: urgent:override a jrame
 
Similar Threads
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
JPanels with different content should have the same size
GUI from eclipse/WindowsBuilder not WYSIWYG
how to populate a JTable from database
Frames