Page 738 - Introduction to Programming with Java: A Problem Solving Approach
P. 738
704 Chapter 17 GUI Programming—Component Layout, Additional GUI Components
Once again, the dashed lines don’t appear on the actual window. We’ve drawn them to show you the region boundaries. There’s no point in applying center alignment to the west and east labels. For these la- bels, the alignment is irrelevant because the west and east labels have no room to move. As evidenced by the dashed lines, they’re already aligned with both their left and right boundaries.
Now back to the alignment constants—SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT. You might think that SwingConstants is a class since its first letter is capitalized. If it were a class, then it would describe an object. But it doesn’t describe an object, and it’s not a class. Actually, SwingConstants is an interface, defined in the javax.swing package. Sun provides the SwingConstants interface as a repository for various GUI-related named constants. To access a named constant in the SwingConstants interface, preface the named constant with the interface name. For example, to access the LEFT alignment constant, use SwingConstants.LEFT. If you want addi- tional details about interfaces, see Chapter 13, Section 13.9.
It’s easy to get confused between label alignment for a BorderLayout container and label align- ment for a FlowLayout container. With a BorderLayout container, if you want to specify a label’s alignment, you need to specify a SwingConstants value as part of the label’s instantiation. If you do that with a FlowLayout container, the code will compile, but it won’t impact the label’s alignment. With a FlowLayout container, individual component alignment is irrelevant. What matters is the container’s alignment. If the container uses left alignment, then all of its components are left aligned; if the container uses center alignment, then all of its components are center aligned; and so on. To set the container’s alignment, insert one of the FlowLayout alignment constants (FlowLayout.LEFT, FlowLayout .CENTER, FlowLayout.RIGHT) in the FlowLayout constructor call. Here’s how to specify left align- ment for all the components in a FlowLayout container:
Apago PDF Enhancer
setLayout(new FlowLayout(FlowLayout.LEFT));
17.5 GridLayout Manager
The BorderLayout manager’s partitioning scheme (north, south, east, west, center) works well for many situations, but not for all situations. Often, you’ll need to display information using a table format; that is, you’ll need to display information that’s organized by rows and columns. The BorderLayout manager doesn’t work well for table formats, but the GridLayout manager works great!
GridLayout Cells
The GridLayout manager lays out a container’s components in a rectangular grid. The grid is divided into equal-sized cells. Each cell can hold only one component.
Assume that you’re inside a container class. To assign a GridLayout manager to the container, call the setLayout method like this:
setLayout(new GridLayout(<number-of-rows>, <number-of-columns>, <horizontal-gap>, <vertical-gap>));
The <number-of-rows> and <number-of-columns> arguments specify the number of rows and number of columns, respectively, in the rectangular grid. The <horizontal- gap> argument specifies the number of pix- els of blank space that appear between each column in the grid. The <vertical-gap> argument specifies the number of pixels of blank space that appear between each row in the grid. If you omit the gap arguments, the gap values are zero by default. In other words, if you call the GridLayout constructor with only two arguments, there will be no gaps between the cells.