File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes vallidate(),pack(),doLayout(),repaint() and setSize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "vallidate(),pack(),doLayout(),repaint() and setSize()" Watch "vallidate(),pack(),doLayout(),repaint() and setSize()" New topic
Author

vallidate(),pack(),doLayout(),repaint() and setSize()

John Kilbourne
Ranch Hand

Joined: Aug 22, 2001
Posts: 30
Can somebody describe the differences and relationships among these methods? I have had a very difficult time getting a scrollpane to size correctly.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120


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


SCJP2. Please Indent your code using UBB Code
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Jose,
you wrote:

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).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Though I said a Container.pack(), but there is no such method. Window.pack() yes it does exits.
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Thanks Jose.
I see the consequential line
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: vallidate(),pack(),doLayout(),repaint() and setSize()