• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent:override a jrame

 
oghenekaro EFEKODO
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11073
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ease Up. We'd appreciate it if you would edit your post and remove Urgent from the subject.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic