I have designed a Swing form. It consists of a JScrollPane that itself contains another JPanel. Due to some UI event a Component gets appended to this JPanel. Its simply like a message box, in which messages gets appended to the list as they arrive.
The question is how to make the change onto this JPanel visible. To, achieve this, I simple call the JFrame#pack() method. But calling this resizes my frame to the size of container, ignoring the prefferedSize, and maximumSize bound I initially set. And Scroll Bars also don’t get Visible, simply the frame gets bigger in height as component gets appended to the panel. I want that my Frame should remain in its initial size, and scrollbars should come into action, when total size of component exceeds the size of frame. Where am I going wrong ? Please Help.
This is my initial screen:

This screen is when component gets added:

When you add components to a panel the general code is:
The revalidate() method will recalculate the preferred size of the panel. If the preferred size is greater than the size of the scrollpane then scrollbars will appear.
Of course you must be using layout managers for this functionality to happen automatically.