aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Positioning dialogs on screen... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Positioning dialogs on screen..." Watch "Positioning dialogs on screen..." New topic
Author

Positioning dialogs on screen...

Igor Romanov
Greenhorn

Joined: Nov 24, 2001
Posts: 23
Sometimes you need to display a dialog window on screen, so that it will be center-aligned on parent's window. But how to do this if you have no reference to parent window, for example when overriding actionPerformed() method. Now I doing it like that:
if( e.getSource() instanceof Component )
{
Component comp = (Component) e.getSource();
Frame[] frms = Frame.getFrames();
for( int i=0; i<frms.length; i++ )
{
if( frms[i].isAncestorOf( comp ) )
{
af = frms[i];
break;
}
}
}
Is it ok, or there's a better way?


cheers,<br />Igor Romanov
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I've always used -


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Igor Romanov
Greenhorn

Joined: Nov 24, 2001
Posts: 23
Thanks for a hint!
Igor Romanov
Greenhorn

Joined: Nov 24, 2001
Posts: 23
It seems, that this will not help if JMenuItem is a source object...
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The reason it doesn't work is because the getWindowAncestor() method just does a getParent() until it hits a Window or null. The problem is, for MenuItems, a JPopupMenu interrupts the getParent() hierarchy, so getWindowAncestor() hits null. There should probably be either a test for JPopupMenu in the getWindowAncestor() code somewhere to fix this, but you can also use this workaround -
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Positioning dialogs on screen...
 
Similar Threads
add method
AWT qustion
an awt question
one question about container
urgent:override a jrame