Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fix JDialog position relative to its parent JFrame component

 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2618
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic