aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes fix JDialog position relative to its parent JFrame component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "fix JDialog position relative to its parent JFrame component" Watch "fix JDialog position relative to its parent JFrame component" New topic
Author

fix JDialog position relative to its parent JFrame component

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi all,

I was wondering if there is an elegant to solution to implement the following feature:
-there is a pop up JDialog window in my application which have as parent (owner) the JFrame main window of my application
-I want this JDialog position/location to be fixed relative to my main JFrame. it means if user moves the main JFrame then the JDialog window will need to move also accordingly so to keep its original position/location relative to main JFrame.

i wonder if this is easy and possible thing to do ? has someone tried before to do such a thing?

thanks much
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

I think you can use a ComponentListener, and listen to its componentMoved event. Unfortunately ComponentEvent does not store the previous location, so you'll have to do a little more effort to calculate the new position. Perhaps storing the previous frame position can solve this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Rob Prime wrote:I think you can use a ComponentListener, and listen to its componentMoved event. Unfortunately ComponentEvent does not store the previous location, so you'll have to do a little more effort to calculate the new position. Perhaps storing the previous frame position can solve this.


thanks ,
i tried this solution but it works with some drawbacks . when i 'm moving the parent JFrame the JDialog doesn't move to adjust for new position. when i'm done moving parent JFrame i release mouse and JDialog stays at old position. when i click on JFrame then at this moment the JDialog moves to new position i calculated inside componentMoved() method.

what i'm looking for is : while the JFrame is being moved, the JDialog should simultaneously move to new position ; giving user the feel that JDialog is sticked to parent JFrame window.
how can i achieve this behavior? the ComponentListener doesn't provide methods to achieve this. is there another EventListener that could be used to track event of JFrame being moved and not JFrame finished moving?

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Can you show us your code? Because I just tried a simple ComponentListener with a JFrame that only prints out the event, and it fires a lot of events while I'm moving the JFrame.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Rob Prime wrote:Can you show us your code? Because I just tried a simple ComponentListener with a JFrame that only prints out the event, and it fires a lot of events while I'm moving the JFrame.

I register the ComponentListener on the JDialog not JFrame. then implement the componentMoved() method to reposition the JDialog..but i got the behavior i explained above.
does the ComponentListener needs to be registered with JFrame? not the JDialog? my JFrame doesn't have a reference to JDialog pop up window so i was obliged to add ComponentListener to JDialog.
I hope someone here could suggest a good solution to problem i described.

thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
is there any need to access the frame whilst the dialog is showing?

if not, make the dialog modal, then the frame can't be accessed, so it can't be moved
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2095
    
    7
does the ComponentListener needs to be registered with JFrame?


Your requirement was:

it means if user moves the main JFrame then the JDialog window will need to move also accordingly


my JFrame doesn't have a reference to JDialog pop up window so i was obliged to add ComponentListener to JDialog.


Well you need to change your design. If you want the two to move in sync, the frame needs to know about the dialog.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Rob Camick wrote:
my JFrame doesn't have a reference to JDialog pop up window so i was obliged to add ComponentListener to JDialog.

Well you need to change your design. If you want the two to move in sync, the frame needs to know about the dialog.

The JFrame doesn't necessarily need to know about the JDialog, but the ComponentListener most certainly needs to know both. Seeing as probably some code from the JFrame creates and shows the JDialog that shouldn't be a problem. And if it is, there's always Window.getWindows().

Yahyia, the ComponentListener definitely needs to be added to the JFrame, because you want to know when the JFrame is moved around. That's when you want to move the JDialog as well. And a ComponentListener listens for move events of the component it was added to - the JFrame.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Rob Prime wrote:
Rob Camick wrote:
my JFrame doesn't have a reference to JDialog pop up window so i was obliged to add ComponentListener to JDialog.

Well you need to change your design. If you want the two to move in sync, the frame needs to know about the dialog.

The JFrame doesn't necessarily need to know about the JDialog, but the ComponentListener most certainly needs to know both. Seeing as probably some code from the JFrame creates and shows the JDialog that shouldn't be a problem. And if it is, there's always Window.getWindows().

Yahyia, the ComponentListener definitely needs to be added to the JFrame, because you want to know when the JFrame is moved around. That's when you want to move the JDialog as well. And a ComponentListener listens for move events of the component it was added to - the JFrame.


thank you guys, i managed to have it partially work by registering the ComponentListener on JFrame (not on JDialog as i did before). the JDialog now moves when i move its parent JFrame. even though the rendering is not satisfactory . user doesn't have the feel that the JDialog is locked/sticked to main JFrame. the user can still feel the JDialog moved after the JFrame moves for a fraction of milliseconds. I wonder if there is a trick to give user feel that this undecorated JDialog is locked to JFrame screen when user moves the main JFrame?

thanks.
 
Don't get me started about those stupid light bulbs.
 
subject: fix JDialog position relative to its parent JFrame component
 
Similar Threads
Correct behaviour when launching a new window and getting rid of the first window
how to position dialogs relative to their parent frame
Mac Java runtime, JDialog getting stuck behind parent JDialog
Finding X Y location of button
JFrame