aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with setLocation() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with setLocation()" Watch "Problem with setLocation()" New topic
Author

Problem with setLocation()

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
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

Joined: Jun 13, 2009
Posts: 2213
    
    7
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jan 28, 2010
Posts: 84
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

Joined: Jun 13, 2009
Posts: 2213
    
    7
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

Joined: Jan 28, 2010
Posts: 84
You're right, that's how I look at it too. I should have added "surprisingly" to my previous statement. Thanks for your comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with setLocation()