1

I have a resizable JScrollPane inside a JDialog. That JScrollPane has a JPanel(called container) All these JScrollPane,JPanel and Jdialog are resizable. At the run time, another JPanel(called child) is added to the previous jPanel which is inside the JScrollPane of the JDialog.I want to get the size of the 'child' JPanel size. Before and after adding the 'child' JPanel to the 'container' JPanel, sizes of 'child' and 'container' JPanels are shown as 0 0. (child.getSize() , container.getSize()) .

Before viewing a JComponent, getting size could be impossible. But in my case even though the 'child' JPanel is added and viewed, sizes are still 0.

How I can get the real size of the 'child' JPanel after adding to the 'container' JPanel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Débora
  • 5,816
  • 28
  • 99
  • 171
  • 5
    Questions of this type generally boil down to fixing problems with an inability to use layouts. The best fix is to learn how to use layouts. – Andrew Thompson Sep 01 '12 at 15:27
  • 1
    By **default** components have a 0x0 size. We need to jump through a few hoops to make it otherwise. See [this thread](http://stackoverflow.com/q/7369814/418556) for some tips on doing it for a `JTable`, though [LabelRenderTest.java](http://stackoverflow.com/a/5853992/418556) does it for a `JLabel` more simply. – Andrew Thompson Sep 01 '12 at 15:36
  • I have not offered an answer. Though if you liked my Qs & As in the links, feel free to up-vote them. ;) – Andrew Thompson Sep 02 '12 at 06:32

2 Answers2

7

You may be able to use validate() before setVisible() in order to get the geometry, as discussed here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

after adding the child Jpanel to the container JPanel, I got the child panel size, then it is not 0 sized, and the real size resulted. Thanks for all who commented and answered for my question. Also, as trashgod said, I called validate() . every thing worked fine.

Débora
  • 5,816
  • 28
  • 99
  • 171