wchargin
wchargin

Reputation: 16027

Automatically set minimum size of JPanel

I have a subclass of JPanel, and I want it to, as soon as its parent frame is pack()ed, to set its minimum size to its new preferred size. I've tried using a ComponentAdapter, but JPanels are by default visible; if I setVisible(false) at the beginning of the constructor, the JFrame won't make it visible again. If I use SwingUtilities's method to get the window root, it will return null because it's in a constructor.

Is there a way to do this?

Upvotes: 1

Views: 3407

Answers (2)

Robin
Robin

Reputation: 36601

to set its minimum size to its new preferred size

Certainly possible, but whether it is useful ... . You can override the getMininumSize() method to return super.getPreferredSize();

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168815

I have a subclass of JPanel, and I want it to, as soon as its parent frame is pack()ed, to set its minimum size to its new preferred size.

If you know the preferred/minimum size before the pack, set it then.

Upvotes: 5

Related Questions