Chapter 16. Layout Managers
A layout manager arranges the child components of a container, as shown in Figure 16.1. It positions and sets the size of components within the container’s display area according to a particular layout scheme. The layout manager’s job is to fit the components into the available area while maintaining the proper spatial relationships among the components. Swing comes with a few standard layout managers that will collectively handle most situations; you can make your own layout managers if you have special requirements.

Figure 16-1. A layout manager at work
Every container has a default layout manager; therefore, when you
make a new container, it comes with a
LayoutManager object of the appropriate type. You
can install a new layout manager at any time with
the
setLayout( ) method. For example, we can set the
layout manager of a container to a BorderLayout:
setLayout (new BorderLayout( ));
Notice that we call the BorderLayout constructor,
but we don’t even save a reference to the layout manager. This
is typical; once you have installed a layout manager, it does its
work behind the scenes, interacting with the container. You rarely
call the layout manager’s methods directly, so you don’t
usually need a reference (a notable exception is
CardLayout). However, you do need to know what the layout manager is going to do with your components as you work with them. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access