Java Reference
In-Depth Information
If you create your own class that implements LayoutManager , you must define all five.
As you will see, many of the methods do not need to do anything, but you must still include
a stub with the appropriate signature.
The addLayoutComponent() method is called only when you add components by calling the
add(String, Component) or add(Component, Object) method, not just plain add(Component) .
For add(Component, Object) , the Object must be of type String , or else that isn't called either.
Exploring the LayoutManager2 Interface
For layout managers that require each component to carry its layout manager constraints, the
LayoutManager2 interface comes into play. The layout managers that use LayoutManager2 include
BorderLayout , CardLayout , and GridBagLayout , to name a few.
LayoutManager2 has five additional methods:
public interface LayoutManager2 {
public void addLayoutComponent(Component comp, Object constraints);
public float getLayoutAlignmentX(Container target);
public float getLayoutAlignmentY(Container target);
public void invalidateLayout(Container target);
public Dimension maximumLayoutSize(Container target);
}
The addLayoutComponent() method is called when you assign constraints to the component
when adding it to the layout. In practice, this means that you added the component to the
container by calling the add(Component component, Object constraints) or add(String name,
Component component) methods, rather than the add(Component component) method. It is up to
the layout manager to decide what, if anything, to do with the constraints. For example,
GridBagLayout uses constraints to associate a GridBagConstraints object to the component
added, and BorderLayout uses constraints to associate a location (like BorderLayout.CENTER )
with the component.
FlowLayout Class
FlowLayout is the default layout manager for a JPanel . A FlowLayout adds components to the
container in rows, working in the order defined by the getComponentOrientation() method of
Component , typically left to right in the United States and western Europe. When it can't fit more
components in a row, it starts a new row, similar to a word processor with word wrap enabled.
When the container is resized, the components within it are repositioned based on the container's
new size. Components within a FlowLayout -managed container are given their preferred size.
If there is insufficient space, you do not see all the components, as illustrated in Figure 10-1.
Search WWH ::




Custom Search