This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with setLocation()

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The setLocation(x,y) method is meant to move the component to the point specified by x and y in the the paren'ts coordinate system.

I created a JWindow and passed a JFrame to it as it's parent. However, the JWindow appears at the point (x,y) relative to the screen's top left corner (the screen's coordinate system).

I also tried calling setVisible() before the call to setLocation(), but that didn't help. What's wrong?
 
Rob Camick
Ranch Hand
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the JWindow appears at the point (x,y) relative to the screen's top left corner (the screen's coordinate system).


Yes, thats correct, the location of top level components like JFrame and JDialog and JWindow is relative to the desktop.

The JFrame is NOT the parent of the JWindow.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The setLocation(x,y) method is meant to move the component to the point specified by x and y in the the paren'ts coordinate system.

that is true for 'added' components.

try adding a JWindow to a JFrame, it will error.

exactly where do you want the window showing?
if it's above the frame, you could use
window.setLocationRelativeTo(frame);

 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
the JWindow appears at the point (x,y) relative to the screen's top left corner (the screen's coordinate system).


Yes, thats correct, the location of top level components like JFrame and JDialog and JWindow is relative to the desktop..


Yeah, I ended up converting the locations that mattered to me to locations relative to the desktop using getLocationOnScreen(), and claculated the JWindow's location subsequently.

Rob Camick wrote:
The JFrame is NOT the parent of the JWindow.


I'm not sure, because myWindow.getParent() returns the JFrame instance passed to it.
 
Rob Camick
Ranch Hand
Posts: 2564
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marwen Bakkar wrote:I'm not sure, because myWindow.getParent() returns the JFrame instance passed to it.


Interesting, however, the API description for the getParent() method says:

"the parent container of this component"

and the frame is obviously not a container for the window. In other words if you use the Container.getComponents() method of the frame you will not get the JWindow as a Component.

You can use parent to mean whatever you want, but I use it to mean the direct ancestor of a component. If I make a parent component invisible, then all the components added to that component are also not painted. If you make the frame invisible the window is still visible. So I don't consider it to be a parent/child relationship.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, that's how I look at it too. I should have added "surprisingly" to my previous statement. Thanks for your comments.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic