Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setLocationRelativeTo(c)  RSS feed

 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having a bit of a problem setting the location of a jdialog component and was hoping someone wouldn't mind helping

I'm calling setLocationRelativeTo(c), and it isn't centering the dialog relative to the parent, it's centered compared to the desktop.
i'm not really sure why, because the docs (or at least my understanding of them) suggest it should be working.

apologies for not providing a small workable piece of code, but my code is long and unwieldy, and not something i want to subject others to!
Here is the constructor for the dialog (which extends JDialog)


is it anything to do with the fact that the method is inherited from the Window class?

Thanks,

Nick

 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no problems when I try to set a JDialog relative to a frame.

Are you creating and displaying the dialog on the Event Dispatch Thread?

You might have to write an SSCCE that demonstrates the problem.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apologies for not providing a small workable piece of code, but my code is long and unwieldy


Create a proper SSCCE.

So you create a JFrame with a JButton that displays your child dialog. Then you try to center the dialog on the frame.

The entire code will be 15-20 lines of code.



 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'll make one now, thanks.

but by the sounds of things i'm not doing anything particularly wrong - i thought i may have missed something obvious.

@Stephan - I'm using SwingUtilities.invokeLater(new Runnable()) to launch the main frame (i'm not really great at this, so i'm not sure if this is what your question really is!). although i've just noticed in my sscce below i'm not.
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, same thing happens here:

 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the time you're calling setLocationRelativeTo(f), f's position is 0,0 and it doesn't have a size yet.

1) You should create DialogTest on the EDT using SwingUtilities.invokeLater().
2) You should call setLocationRelativeTo(f) before you make the dialog visible.
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:At the time you're calling setLocationRelativeTo(f), f's position is 0,0 and it doesn't have a size yet.

1) You should create DialogTest on the EDT using SwingUtilities.invokeLater().
2) You should call setLocationRelativeTo(f) before you make the dialog visible.


ahh. yeah, that has actually caught me out before (well, something similar at least). i've moved the initialisation of the dialog after the visibility of parent is set:



and it works. is this the best way to do it?

and RE the EDT - yeah, I do normally call the main swing class using the SwingUtilities method. even so, i've definitely got more reading to do!

Thanks for your help, much appreciated!

Nick
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Child dialogs are usually displayed when a user clicks a button or menu item, not automatically when the frame is displayed.

Why not just create the dialog when it is needed (i.e. in the ActionListener)?

The setLocationRelativeTo(...) method only sets the location at the current point in time.

What if the frame is moved before the dialog is ever displayed?

 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, that's a good point. I'm following a tutorial at the moment, so that's where I generally get the design or structure from. it's hard to question it when you're learning

If I created the Dialog in the ActionListener, where would be the best place to create and display its components via gridbag? Should I just extend it rather than defining everything in the anonymous listener?

btw, I found your blog/site on swing a while back - maybe a bit beyond me at the moment but it's definitely been useful, thanks!

Nick
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The components should be created in the constructor of your class, not in the ActionListener.

The question is does your class extend JPanel or JDialog?

This is addressed on the internet by doing a search on "inheritance vs composition".
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, of course, sorry, wasn't thinking straight!

will definitely look that up though. i would (as of right now) make the prefDialog extend JDialog - I assume the alternative is having a JPanel that has a JDialog itself?

...I'm sure I'll find out when googling the difference

again, thanks for the quick help!

Nick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!