1) Setting the preferred size to null should reset the component to get its preferred size, calculated as if it had never been installed.
component.setPreferredSize(null);
This may not do what you want, depending on how you signal that the layout needs to be redone - but at least this is technically the answer to your question.
2) It is generally recommended not to use setPreferredSize, see this post
source share