This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes centering a dialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "centering a dialog" Watch "centering a dialog" New topic
Author

centering a dialog

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in Swing (by Robinson) and in this sun tutorial its claimed:
If you specify a Frame or Dialog, then the Dialog will appear over the center of the Frame (...). If you specify a component inside a Frame, then the Dialog will appear over the center of that component (...). If you specify null, then the look and feel will pick an appropriate position for the dialog — generally the center of the screen (...).

the example i show bellow infirms that:
case A:
frame isnt centered - either using new JFrame() or null the dialog pops up centered relative to screen
case B:
frame is centered - in both cases dialog appears centered relative to screen, as above

my conclusion: dialog allways centers itself relative to screen
what am i missing here?

the code:


java amateur
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
In none of you examples did you actually specify a visble component.

"null" is not a visible component.
"new JFrame()" is not a visible component.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

miguel lisboa wrote:

Check out Window's setLocationRelativeTo method. If you pass null as its argument it will center the window (JFrame is a Window subclass) on the screen taking into account things like the task bar etc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:In none of you examples did you actually specify a visble component.

"null" is not a visible component.
"new JFrame()" is not a visible component.

case solved!

thanks to both
 
 
subject: centering a dialog