How to get the size of JPanel which is not visible yet

◇◆丶佛笑我妖孽 提交于 2019-12-13 00:32:18

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12228992/how-to-get-the-size-of-jpanel-which-is-not-visible-yet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!