Difference between setWidth() and setPreferredWidth() What is the difference between the width property and the preferredWidth property, for a table header ?

Scott Stanchfield

The preferred size of a component (which is made up of preferred width and preferred height) is the component's request for the size it would like to appear. Layout managers may use this or may ignore this when determining what size to set a component.

Note that you should never explicitly set the preferred size of a component; the component should be left to calculate its preferred size, which is usually based on font sizes, images, and text that will appear on the component.

The size of a component (which is made up of width and height) is a the current size of the component. You can set this explicitly, or, if using a layout manager, the manager will set it for you.

I recommend you read http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/index.html for details on how layout managers use preferred size.

Comment and Contribute

 

 

 

 

 


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

 

 

About | Sitemap | Contact