Question

I wanted to ask whether it is possible to get the correct sizes of an JPanel after it has been placed in another JPanel that uses GroupLayout as Layout Manager. I have already tried to use:

.getPreferredSize(): this results in the Preferred Size that has been set by me, not the actual size that is drawn on the JPanel in the frame (if frame get's resized, the element will expand horizontally; which is not seen in the values).

.getSize(): it returns 0.

.getHeight(): it returns 0.

.getWidth(): it returns 0.

Maybe the positioning of the code is relevant, but it is executed AFTER shown on screen so it should not matter.

To force it to do that after it is shown on screen, maybe I can use EventQueue, but I'm not sure how.

Thank you for your answers!

Was it helpful?

Solution

You can get the "correct" size of the component only after it has been rendered, either by calling pack or setVisible(true) on the top level container.

Maybe the positioning of the code is relevant, but it is executed AFTER shown on screen so it should not matter.

Then something's not right. Are you sure that you're calling these methods on the visible components and not some variables that shadow them? Without code it's hard to tell where your error lies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top