Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Using Layout Managers
- Workshop: Laying Out an Application
- Summary
- Q&A
- Quiz
- Activities
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Using Layout Managers
In Java, the placement of components within a container depends on the size of other components and the height and width of the container. The layout of buttons, text fields, and other components can be affected by the following things:
- The size of the container
- The size of other components and containers
- The layout manager that is being used
There are several layout managers you can use to affect how components are shown. The default manager for panels is the FlowLayout class, which was used during the previous hour.
Under FlowLayout, components are dropped onto an area in the same way words are organized on a printed page in English—from left to right, then on to the next line when there's no more space.
To set up a container to work under FlowLayout:
- Create a Container object and a FlowLayout object.
- Set up the container's layout manager by calling the Container object's setLayout() method with the FlowLayout object as an argument.
- Call setContentPane() with the Container as an argument.
The following example could be used in a frame so that it will employ flow layout when components are added:
Container pane = getContentPane(); FlowLayout topLayout = new FlowLayout(); pane.setLayout(topLayout); setContentPane(pane);
You can also set up a layout manager to work within a specific container, such as a JPanel object. You can do this by using the setLayout() method of that container object. The following statements create a JPanel object called inputArea and set it up to use FlowLayout as its layout manager:
JPanel inputArea = new JPanel(); FlowLayout inputLayout = new FlowLayout(); inputArea.setLayout(inputLayout);
To give you an idea of how the different layout managers work, a simple application will be shown under each of the classes. The Crisis application has a graphical user interface with five buttons. Load your word processor and open up a new file called Crisis.java. Enter Listing 14.1 and save the file when you're done.
Example 14.1. The Full Text of Crisis.java
1: import java.awt.*;
2: import javax.swing.*;
3:
4: public class Crisis extends JFrame {
5: JButton panicButton = new JButton("Panic");
6: JButton dontPanicButton = new JButton("Don't Panic");
7: JButton blameButton = new JButton("Blame Others");
8: JButton mediaButton = new JButton("Notify the Media");
9: JButton saveButton = new JButton("Save Yourself");
10:
11: public Crisis() {
12: super("Crisis");
13: setSize(308, 128);
14: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15: Container pane = getContentPane();
16: FlowLayout flo = new FlowLayout();
17: pane.setLayout(flo);
18: pane.add(panicButton);
19: pane.add(dontPanicButton);
20: pane.add(blameButton);
21: pane.add(mediaButton);
22: pane.add(saveButton);
23: setVisible(true);
24: }
25:
26: public static void main(String[] arguments) {
27: Crisis cr = new Crisis();
28: }
29: }
After compiling the Crisis application, you can run it with the following command:
java Crisis
Figure 14.1 shows the application running.
Figure 14.1 Arranging components using flow layout.
The FlowLayout class uses the dimensions of its container as the only guideline for how to lay out components. Resize the window of the application to see how components are instantly rearranged. Make the window twice as wide, and you'll see all of the JButton components are now shown on the same line. Java programs will often behave differently when their display area is resized.
The GridLayout Manager
The GridLayout class organizes all components in a container into a specific number of rows and columns. All components are allocated the same amount of size in the display area, so if you specify a grid that is three columns wide and three rows tall, the container will be divided into nine areas of equal size.
The following statements create a container and set it to use a grid layout that is two columns wide and three rows tall:
Container pane = getContentPane(); GridLayout grid = new GridLayout(2, 3); pane.setLayout(grid);
Figure 14.2 shows what the Crisis application would look like if it used grid layout.
Figure 14.2 Arranging components using grid layout.
Some of the labels in Figure 14.2 display text that has been shortened—if the text is wider than the area available in the component, the label will be shortened using ellipses (…).
GridLayout places all components as they are added into a place on a grid. Components are added from left to right until a row is full, and then the leftmost column of the next grid is filled.
The BorderLayout Manager
The next layout manager left to experiment with is the BorderLayout class. The following statements create a container that uses border layout, placing components at specific locations within the layout:
Container pane = getContentPane(); BorderLayout crisisLayout = new BorderLayout(); pane.setLayout(crisisLayout); pane.add(panicButton, BorderLayout.NORTH); pane.add(dontPanicButton, BorderLayout.SOUTH); pane.add(blameButton, BorderLayout.EAST); pane.add(mediaButton, BorderLayout.WEST); pane.add(saveButton, BorderLayout.CENTER);
Figure 14.3 shows how this looks in the Crisis application.
Figure 14.3 Arranging components using border layout.
The BorderLayout manager arranges components into five areas: four denoted by compass directions and one for the center area. When you add a component under this layout, the add() method includes a second argument to specify where the component should be placed. This argument should be one of five class variables of the BorderLayout class: NORTH, SOUTH, EAST, WEST, and CENTER are used for this argument.
Like the GridLayout class, BorderLayout devotes all available space to the components. The component placed in the center is given all the space that isn't needed for the four border components, so it's usually the largest.
Separating Components with Insets
As you are arranging components within a container, you might want to move components away from the edges of the container. This is accomplished using Insets, an object that represents the border area of a container.
The Insets class, which is part of the java.awt package, has a constructor method that takes four arguments: the space to leave at the top, left, bottom, and right on the container. Each argument is specified using pixels, the same unit of measure employed when defining the size of a frame.
The following statement creates an Insets object:
Insets around = new Insets(10, 6, 10, 3);
The around object represents a container border that is 10 pixels inside the top edge, 6 pixels inside the left, 10 pixels inside the bottom, and 3 pixels inside the right.
To make use of an Insets object in a container, you must override the container's getInsets() method. This method has no arguments and returns an Insets object, as in the following example:
public Insets getInsets() {
Insets squeeze = new Insets(50, 20, 15, 20);
return squeeze;
}
Figure 14.4 shows how this would change one of the preceding examples, the BorderLayout-managed interface shown in Figure 14.2.
Figure 14.4 Using insets to add space around components.
The container shown in Figure 14.4 has an empty border that's 20 pixels from the left edge, 15 pixels from the bottom edge, 20 pixels from the right edge, and 50 pixels from the top edge.
Workshop: Laying Out an Application | Next Section

Account Sign In
View your cart