How to recalculate your preferred JComponent size?

I know that when I create an instance of JComponent, it has its own preferred size. Now suppose that I set PreferredSize to be a JComponent manually with a size of 0 x 0. And I want this component to be "reset" as its own preferred Size. How to do it?

+4
source share
1 answer

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

+8
source

Source: https://habr.com/ru/post/1390373/


All Articles