问题
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