Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing Custom Layout Managers

Status
Not open for further replies.

jsulman

Programmer
Jun 14, 2001
85
US
I am currently wrestling with Swing custom LayoutManagers. The book I am using gives a pretty simple example but I cannot follow the sequence of methods that are fired off when a panel has a layout manager set to it and when a component is added to it. Does anybody know how the methods are fired?
The LayoutManager has 5 methods:

addLayoutComponent(String name, Component comp)
layoutContainer(Container parent),
minimumLayoutSize(Container parent),
preferredLayoutSize(Container parent)
removeLayoutComponent(Component parent)

Which of these are executed when the Layout is set to a panel (p1.setLayout(LayoutManager mgr) and when a component is added to the panel. This would sure make it easier to understand the example I am working through. Or could you direct me to some documentation that would explain this.

Thanks in advance

Jeff Sulman
 
for instance:

panel.setLayout(new BorderLayout());
-> setLayout-method of class Container is executed:

//////////////////////code/////////////////////
layoutMgr = mgr;
if (valid) {
invalidate();
}
///////////////////////////////////////////////

The invalidate-method marks the container and all
of the parent components as possibly "not lay-
outed".

panel.add(new TextArea(),"center");

-> add(Component comp, Object constraints) from
Container-class is executed


//////////////////////code//////////////////////
addImpl(comp, constraints, -1);
////////////////////////////////////////////////


-1 because we didn't provide an index in the add-
message; if you do that you can place the new com
ponent at a certain position in the row of
components of that container.


-> addImpl-method of class Container

/////////////////////////////////////////////////////
protected void addImpl(Component comp, Object
constraints, int index) {
.....

if (valid) {invalidate();}

.....

if (layoutMgr != null) {
layoutMgr.addLayoutComponent((String)constraints,
comp);
}
}
////////////////////////////////////////////////////////

-> addLayoutComponent-methods of class BorderLayout (in
this case)

////////////////////////////////////////////////////////
public void addLayoutComponent(Component comp, Object
constraints) {
...
addLayoutComponent((String)constraints, comp);
...
}
///////////////////////////////////////////////////
public void addLayoutComponent(String name, Component
comp) {
...
if (name == null) {
name = "Center";
}

if ("Center".equals(name)) {
center = comp;
}
...
////////////////////////////////////////////////////////


So, you should notice the container is left in an invalidated state after assiging a layout manager or
adding a component. So you should always perform the
container.validate()-method afterwards.

Greetings,
Swamphen
 
Swamphen,
Thanks for your detailed response. I have two more quick questions. First, where did you get this infor from? The Java Source Code? If so where can I get the JSC?

Second, in most of the custom gridlayouts I notice that the preferredLayoutSize(Component comp) does a lot of the work. How does this fit into the picture? When is it called?

Thanks Again.
 

Java Source
-----------


This link should provide you with the Java Source code:




preferredLayoutSize(Component comp)
-----------------------------------


When you call the validate() method to set the layout of the components of a container right, the following method-sequence arises:

1) container.validate();
2) container.layout();
3) layoutmanager.layoutContainer(container);
4) somewhere along the layouting in that method,
for each of the components in that container:
component.getPreferredSize();
5) if the component is a container itself, this last method
will lead to the invokement of:
thats_components_layoutmanager.preferredLayoutsize(that
_component);

In this last method the preferred sizes of the components in the component/container are used to determine an overal
preferred size of the container for the chosen layout.

I hope things are starting to clear now?

Swamphen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top