This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
doLayout() call the LayoutManager of the Container that holds the Component on which this message was sent, to layout the Component. Normal programs should not call it directly because it is called when the Container receives the validate message. validate() mainly intended to be invoked over Containers. Call doLayout on the Components contained by the Container. pack() on a Container links the Container to its peer. The peer is the native resource for representation on the screen. It does the same to the Components holded by the Container. Also set the sizes to the natural ones. That is those returned by getPreferredSize method. But the size is subjected to LayoutManger oversight. A Window needs either pack or setSize, after that show or setVisible. The last two also links the Java objects to their peers (as pack does) but they don't set the size. I have read that setSize, setBounds and setLocation, for Components contained by a Container, only work if the LayoutManager is null. Calling repaint shedules a call to upgrade in the AWT Painting-Event thread. There is only a thread dedicated to these tasks because otherwise the painting of a Component could be interrupted, and it could be left unfinished. The default implementation for upgrade just clean the Component drawing area and call paint. This method lastly paints the Component. Any corrections are wellcomed
pack() on a Container links the Container to its peer. The peer is the native resource for representation on the screen. It does the same to the Components holded by the Container. Also set the sizes to the natural ones. That is those returned by getPreferredSize method. But the size is subjected to LayoutManger oversight.
1)But how about Swing light-weight components? 2)Then I am perplexed I have never read that pack() links to peer. I have never read it about any method... I always remembered, my books, Sun's API and tutorial, etc. tell:
"Java Programming Language". SL-275. Student Guide. Sun Microsystems. Revision C.1, September 1999 states on the page 8-19:~ f.pack() This method tells the frame to set a size that "neatly encloses" the components that it contains. To determine what size to use for the Frame, f.pack() queries the layout manager, which is responsible for the size and position of all components within the Frame().
pack() makes layout of all components in Container while doLayout() of just one specifically.... [This message has been edited by G Vanin (edited November 20, 2001).]
Joined: Jul 03, 2001
Hi G This from Window.pack() API:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
and this is from Component.isDisplayable() API:
Determines whether this component is displayable. A component is displayable when it is connected to a native screen resource. A component is made displayable either when it is added to a displayable containment hierarchy or when its containment hierarchy is made displayable. A containment hierarchy is made displayable when its ancestor window is either packed or made visible. A component is made undisplayable either when it is removed from a displayable containment hierarchy or when its containment hierarchy is made undisplayable. A containment hierarchy is made undisplayable when its ancestor window is disposed.
Joined: Jul 03, 2001
Though I said a Container.pack(), but there is no such method. Window.pack() yes it does exits.