Java Reference
In-Depth Information
The Flow Layout Manager
The flow layout manager places components in a row, and when the row is full, it automatically spills com-
ponents onto the next row. The default positioning of the row of components is centered in the container, and
the default orientation is from left to right. You have five possible row-positioning options that you specify
by constants of type int that are defined in the FlowLayout class. These are LEFT , RIGHT , CENTER , LEADING ,
and TRAILING . LEADING and TRAILING specify the edge of the component to which the row should be justi-
fied. The effects of the first three on the alignment of a row of components are what you would expect. The
CENTER option is the default. By default, components in a row are separated by a five-unit gap and success-
ive rows are separated by the same distance.
The flow layout manager is very easy to use, so let's jump straight in and see it working in an example.
TRY IT OUT: Using a Flow Layout Manager
As I said earlier, this layout manager is used primarily to arrange a few components whose relative posi-
tion is unimportant. Let's implement a TryFlowLayout program based on the TryWindow4 example:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
public class TryFlowLayout {
// Create the application window
public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
Toolkit theKit = aWindow.getToolkit(); // Get the
window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen
size
// Set the position to screen center & size to half screen size
aWindow.setSize(wndSize.width/2, wndSize.height/2); // Set
window size
aWindow.setLocationRelativeTo(null); // Center
window
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
// Create a layout
manager
Container content = aWindow.getContentPane();// Get the content
pane
content.setLayout(flow);
// Set the
container layout mgr
Search WWH ::




Custom Search