I include a JPanel with a null layout in a JScrollPane, and it doesn't display the scrollbars. Why?

Scott Stanchfield

First, always use layout managers when constructing a GUI! See Effective Layout Management (http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr) for details on why, what layout managers are and how to use them.

Second, a JScrollPane asks the contained component for its preferred size. A JPanel with no layout manager returns (0,0), which tells the JScrollPane that it doesn't need to provide scrollbars.

A JPanel with a layout manager delegates the preferred size request to its layout manager, which computes a size that should make all the contained components happy. When this, (usually) non-zero size is returned, the JScrollPane will provide scrollbars if there is not enough room.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.