I know the fact that when I create an instance of a JComponent, it has it's own preferred size. Now let's suppose that I setPreferredSize that JComponent manually with a dimension of 0 x 0. And i want that Component to "reset" its own preferredSize. How do I do that?
How do i recalculate preferred size of a JComponent?
Asked Answered
1) Setting preferred size to null should reset the component back to getting its preferred size calculated as if it was never set.
component.setPreferredSize(null);
This might not do what you want, depending on how you signal that the layout should be redone - but at least it is technically the answer to your question.
2) It is generally advised to not use setPreferredSize, see this post
Ah! You're a lifesaver. Thanks man! I should've read the documentation first. The problem is that the layout manager require me to specify the preferred size because it needs it to calculate the space. –
Oxalate
© 2022 - 2024 — McMap. All rights reserved.
setVisible(false)
might be the effect you need. – Alderson