I'm having difficulty determining the correct size of a JDesktopPane. The following code tells me that the size of the pane is 0 x 0

//Inside a JFrame
JDesktopPane myDesktop = new JDesktopPane();
this.setContentPane(myDesktop);
System.out.println(myDesktop.getSize());

However, after looking in the source for GridLayout.java, virtually identical code is used.

//Inside GridLayout.java, except I added the println() and omitted stuff
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
System.out.println(parent.getSize());
}
}

parent is the JDesktopPane that is being layed out.

The best I can figure is that somewhere in the innards of Swing/AWT the size of the JDesktopPane is being changed before getting passed to the LayoutManager. If I recall correctly, Java passes objects by reference. My next idea was that perhaps after applying the layout the size gets changed, so I tried the following:

//Inside the same JFrame
//After stuff that takes up space is added.
myDesktop.setLayout(new GridLayout(2,2));
System.out.println(myDesktop.getSize());

But no luck. As I understand it, this means, the somewhere a copy of the JDesktoPane is getting made, modified, and passed to the layout manager, but not original one.

Am I even remotely close? Can anybody tell me how I can access the actual size of the JDesktopPane?

Recommended Answers

All 5 Replies

Are these JInternalFrames that your adding to the JDesktopPane?

Are these JInternalFrames that your adding to the JDesktopPane?

Yes. My appologies for the confusion. I have a JFrame object with a JDesktopPane. I'm trying to add 4 JInternalFrames for the 3 axes of a 3D scene, plus one camera frame. I need to know the size of the JDesktopPane so I can set the initial size of the JInternalFrames to fill the screen. After that it doesn't matter, because I want the user to be able to move the JInternalFrames at will. Using GridLayout does not behave appropriately, because although it gets the initial positions right, when I try to drag one of the JInternalFrames, the other two reajust themselves in the GridLayout.

You have to set the size of the JInternalFrames before you add them to the JDesktopPane. If you don't, then you will get 0 size for the JDesktopPane and it wont show up.
I wouldn't worrry about the size of the JDesktopPane. Why don't you try not setting the size of the JDesktopPane and see if it will resize automatically to the sizes of the JInternalFrames.
Also, all layout managers will resize or reposition the components when somethings changed. You could try to set the layout manager to null and use the setbounds() method to set the position of the frames, but I don't know how that will work.

You have to set the size of the JInternalFrames before you add them to the JDesktopPane. If you don't, then you will get 0 size for the JDesktopPane and it wont show up.
I wouldn't worrry about the size of the JDesktopPane. Why don't you try not setting the size of the JDesktopPane and see if it will resize automatically to the sizes of the JInternalFrames.
Also, all layout managers will resize or reposition the components when somethings changed. You could try to set the layout manager to null and use the setbounds() method to set the position of the frames, but I don't know how that will work.

Even when I set the size of the JInternalFrames, the JDesktopPane still seems to think it has a 0x0 size. When I apply a GridLayout to the JDesktopPane it thinks it has a 0x0 size too. That last part is exactly what I'm trying to do, but I don't know what values to use because I don't know the size of the JDesktopPane.

What about when you run the program, does nothing show up, or does the DesktopPane seem to have some size?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.