This section describes some possible problems with layout managers and provides workarounds when available.
Issue 1: Call to invalidate()
and validate()
increases component size
Cause: Due to some specifics of the GridBagLayout
layout manager, if ipadx
or ipady
is set, and invalidate()
and validate()
are called, then the size of the component increases to the value of ipadx
or ipady
. This happens because the GridBagLayout
layout manager iteratively calculates the amount of space needed to store the component within the container.
Workaround: The JDK does not provide a reliable and simple way to detect if the layout manager should rearrange components or not in such a case, but there is a very simple workaround. Use components with the overridden method getPreferredSize()
, which always returns the current needed size as shown in Example 10-1.
Issue 2: Infinite recursion with validate()
from any Container.doLayout()
method
Cause: Invoking validate()
from any Container.doLayout()
method can lead to infinite recursion because AWT itself invokes doLayout()
from validate()
.