How to Use FlowLayout

 <  Day Day Up  >  

The FlowLayout [14] class provides a very simple layout manager that is used, by default, by JPanel s. Figure 16 shows an example.

[14] FlowLayout API documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/FlowLayout.html.

Figure 16. A simple GUI that uses FlowLayout .

graphics/08fig19.gif

graphics/cd_icon.gif

You can run FlowLayoutDemo using Java Web Start or compile and run the example yourself. [15]

[15] To run FlowLayoutDemo using Java Web Start, click the FlowLayoutDemo link in the RunExamples/layout.html page on the CD. You can find the source files here: JavaTutorial/uiswing/layout/ example-1dot4/index.html#FlowLayoutDemo .

FlowLayout puts components in a row, at their preferred size . If the horizontal space in the container is too small to put all of the components in one row, FlowLayout uses multiple rows. If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within it. You can specify that it stick to the left or right side instead by using a FlowLayout constructor that takes an alignment argument. You can also specify how much vertical or horizontal padding is put around the components.

Note: FlowLayout never requests more height than is required for one row. For this reason, we don't recommend using FlowLayout if, for some reason, the FlowLayout 's container might start out with less horizontal space than it needs to fit all of the components in a single row.


The following code from FlowLayoutDemo.java creates the FlowLayout and the components it manages :

 contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("Button 2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("5")); 

The FlowLayout API

Table 7 lists the three constructors of the the FlowLayout class. You can find the FlowLayout API documentation online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/FlowLayout.html.

Table 7. FlowLayout Constructors

Constructor

Purpose

 public FlowLayout() public FlowLayout(int alignment) public FlowLayout(int alignment,                   int horizontalGap,                   int verticalGap) 

Create a FlowLayout instance. The alignment argument can be FlowLayout.LEADING , FlowLayout.CENTER , or FlowLayout.TRAILING . When FlowLayout controls a container with a left-to-right component orientation (the default), LEADING specifies left alignment and TRAILING specifies right alignment. The horizontalGap and verticalGap arguments specify the number of pixels between components. (The default gap value is 5.)

Examples That Use FlowLayout

The following table lists some of the examples that use FlowLayout .

Example

Where Described

Notes

FlowLayoutDemo

This section

Sets up a content pane to use FlowLayout . If you set the RIGHT_TO_LEFT constant to true and recompile, you can see how FlowLayout handles a container that has a right-to-left component orientation.

CardLayoutDemo

How to Use CardLayout (page 476)

Centers a component in the top part of a BorderLayout , by putting the component in a JPanel that uses a FlowLayout .

ButtonDemo

How to Use Buttons (page 156)

Uses the default FlowLayout of a JPanel .

TextInputDemo

How to Use Formatted Text Fields (page 221)

Uses a panel with right-aligned FlowLayout to present two buttons.

 <  Day Day Up  >  


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net