Java Reference
In-Depth Information
GridLayout places components in the container in a rectangular grid with the number of rows and
columns that you specify.
GridBagLayout also places the components into an arrangement of rows and columns, but the
rows and columns can vary in length. This is a complicated layout manager with a lot of flexibility
in how you control where components are placed in a container.
BoxLayout arranges components either in a row or in a column. In either case the components are
clipped to fit if necessary, rather than wrapping to the next row or column. The BoxLayout man-
ager is the default for the Box container class.
SpringLayout allows components to have their positions defined by “springs" or “struts" fixed to
an edge of the container or other components in the container.
The BoxLayout , SpringLayout , and Box classes are defined in the javax.swing package. The other lay-
out manager classes in the preceding list are defined in java.awt .
One question you might ask is why do you need layout managers at all? Why can't you just place com-
ponents at some given position in a container? The basic reason is to ensure that the GUI elements for your
Java program are displayed properly in every possible Java environment. Layout managers automatically
adjust the size and positions of components to fit within the space available. If you fix the size and position
of each of the components, they could run into one another and overlap if the screen area available to your
application window is reduced.
You call the setLayout() method for a Container object to set the layout manager. For example, you
could change the layout manager for the JFrame object aWindow from its default BorderLayout layout man-
ager to flow layout with the following statements:
FlowLayout flow = new FlowLayout();
aWindow.getContentPane().setLayout(flow);
Remember that components that you want to display in the client area of a JFrame object should be added
to its content pane. The same goes for JDialog and JApplet objects. In fact, if you use the add() meth-
od for a JFrame , JDialog , or JApplet object to add a component, it is redirected to the content pane, so
everything is as it should be. This convenience facility is there for consistency with similar AWT compon-
ents. However, for some other operations — setting the background color, for example — you must call the
method belonging to the content pane object for things to turn out as you expect. For this reason I think it's
better to explicitly perform all operations on the content pane rather than rely on redirection by the parent
frame object. That way you won't forget that it's the content pane you are working with.
With some containers you can set the layout manager in the constructor for that container, as you see in
later examples. Let's look at how the layout managers work, and some examples of how you might use them
in practice.
NOTE Several Java IDEs provide a great deal of support for laying out components and re-
lieve you of the drudgery of working with the detail of a flow layout manager. However, it is
still useful to understand what is going on.
Search WWH ::




Custom Search