Guidelines for Using Layout Managers
Now that we've examined the advantages and disadvantages of the layout managers included with Java, it's
appropriate to discuss some more general topics related to how to use layout managers.
Combining Layout Managers
In the previous discussions of layout managers, each one was treated independently of the other, but it's
common practice for a user interface to use multiple layout managers. In fact, you'll often find it necessary or
desirable to create a container that uses one type of layout manager and add child containers to that parent
which use different types of layout managers. For example, suppose that you want to create a user interface
like the one shown below. In this case, the component at the top is displayed using its preferred height and
fills the width of the container. In addition, a row of buttons that are equal in size occupies the bottom, and a
component in the center fills the remaining area.

To some extent, BorderLayout provides the functionality needed to create this component, but you cannot
use it directly to create the bottom row of buttons. That's because BorderLayout allows only a single
component to be added to a location, such as the SOUTH portion of its container. This problem can be
resolved by adding the two buttons to a container such as a JPanel and adding that panel to the parent
managed by a BorderLayout. Since the buttons should be given the same size, GridLayout is the
obvious choice for the container that the buttons will be added to, and the code to implement this is
shown below:
import java.awt.*;
import javax.swing.*;
public class Embedded extends JFrame {
public static void main(String[] args) {
Embedded e = new Embedded();
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setSize(400, 300);
e.setVisible(true);
}
public Embedded() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(getHeader(), BorderLayout.NORTH);
pane.add(getTextArea(), BorderLayout.CENTER);
pane.add(getButtonPanel(), BorderLayout.SOUTH);
}
protected JComponent getHeader() {
JLabel label = new JLabel("Embedded Layout Manager Test",
JLabel.CENTER);
label.setFont(new Font("Courier", Font.BOLD, 24));
return label;
}
protected JComponent getTextArea() {
return new JTextArea(10, 10);
}
protected JComponent getButtonPanel() {
JPanel inner = new JPanel();
inner.setLayout(new GridLayout(1, 2, 10, 0));
inner.add(new JButton("Ok"));
inner.add(new JButton("Cancel"));
return inner;
}
}
As shown below, this code doesn't quite achieve the desired results, since the buttons have been stretched to
fill the width of the container:
That's because the buttons' parent container is stretched by the BorderLayout so that its width is equal to
the width of the frame, and that in turn causes the GridLayout to stretch the buttons to fill their parent
container. To fix this problem, it's necessary to put the panel managed by the GridLayout into another
container that won't stretch it. Since FlowLayout always displays components using their preferred size, we
can use it to provide this behavior, so we'll define an additional FlowLayout-managed JPanel, add the
button panel to it, and add the button panel to the content pane:
protected JComponent getButtonPanel() {
JPanel inner = new JPanel();
inner.setLayout(new GridLayout(1, 2, 10, 0));
inner.add(new JButton("Ok"));
inner.add(new JButton("Cancel"));
JPanel outer = new JPanel();
outer.setLayout(new FlowLayout());
outer.add(inner);
return outer;
// return inner;
}
Finally, running this modified code produces the desired interface that was illustrated at the start of this section.
You'll often find it necessary to embed containers within other containers and to use different layout
managers when doing so. If you're creating a complex user interface, it's often helpful to conceptually break
the interface down into smaller, simpler portions that can be created using the existing layout managers.
Those smaller pieces can then be created and combined into the large, complex interface instead of trying to
produce the desired results with a single layout manager.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|