Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JDialog positioning  RSS feed

 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going crazy trying to place a JDialog box near the JButton that initiated the instantiation of the Dialog, but not obscuring any part of the JButton. All the code that has anything to do with this seems to be broken. I've tried calculating the location of the dialog, but when I call setLocation it never puts it in the right place relative to the JButton. calling setLocationRelativeTo on the dialog produces marginally better results, but the dialog still refused to appear obscuring any part of the JPanel containing the JButton, so in some cases that could leave the dialog quite a distance away from the JButton.

Does anybody know how to get the dialog and the JButton into the same coordinate system?
 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I got it...

component.getLocationOnScreen()

is what is necessary to position the dialog correctly with respect to the component.
 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess calling getlocation() returns the position relative to the parent container.
 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it my imagination, or is this the perfect illustration of how important is the skill of naming? getLocation() should return the "default" location, or the location on the screen. And the method that returns the location of a component relative to its parent container should have the word "relative" in it. getRelativeLocation() or something like that.

Well... it's free code... I'm sure not going to complain. Just ruminating about a skill I've always thought to be much more important than you might think.

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, at least it's consistent, as far as I know. That is, everywhere in Swing, "location" refers to the relative location, so it's "just" terminology you need to learn.

It's far worse when the same term refers to different things in the same context.
 
Joe McIntyre
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool... I know just enough swing to be dangerous, so no, I didn't know that... thanks
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing that helps, Wally, is reading the API when you run across these kinds of issues. They usually do a good job of clearing up confusing method names.

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html#setLocation(int,%20int)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!