2

I have a JLayeredPane, and inside the JLayeredPane is a JInternalFrame. What I need are the bounds (x position, y position, width, height) of the content pane in the JInternalFrame. The bounds need to be in relation to the JLayeredPane. My problem is the borders, title bar, and I'm not sure what else of the JInternalFrame are messing with my calculation. It's a few pixels off.

Here is how I try to calculate it. This code is in the JInternalFrame:


Rectangle area = new Rectangle(
                getX() + 2,  //The x coordinate of the JInternalFrame + 2 for the border
                getY() + ((javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).getNorthPane().getHeight(), //The Y position of the JinternalFrame + theheight of the title bar of the JInternalFrame
                getContentPane().getWidth() - 2, //The width of the Jinternals content pane - the border of the frame
                getContentPane().getHeight() //the height of the JinternalFrames content pane
                );

I need to get the location of the content pane of the internal frame. alt text

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
user489041
  • 27,916
  • 55
  • 135
  • 204

2 Answers2

6

Coordinates with respect to what? The screen? The window?

Calling getLocation() will give you the coordinates relative to the component's parent in the window hierarchy.

SwingUtilities has several methods for transforming coordinates from one frame of reference to another. So, to compensate for the title bar and frame, you can do this:

JInternalFrame iframe = ...
Container c = iframe.getContentPane();
Rectangle r = c.getBounds();
r = SwingUtilities.convertRectangle(c.getParent(), r, iframe.getParent());
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
2

The getBounds() method of JInternalFrame returns a Rectangle with the coordinates you need. Why not just do the following:

{
      ...
      JInternalFrame iframe = ...;
      Rectangle r = new Rectangle(iframe.getBounds());
}

This way you don't need to manually calculate the bounds.

  • The issue with this is this will give me the bounds that includes the title bar of the internal frame and also the border. I need the bounds of the content pane only. But i need the x coordinate and the y coordinate to the the location of the content pane in the JLayeredPane. – user489041 Nov 24 '10 at 19:48