Java: Flow Layout Manager (swing)

Contributor Icon Contributed by William_Wilson Date Icon February 20, 2006  
Tag Icon Tagged: Java programming

how to use the Flow Layout Manager


Layout Managers are used in the orginization of Panels and Frames. The proper layout should be chosen to accomodate, frame resizings and use.

+ signs represent empty space.

Flow:
—————————
| Frame Title |▄|▐ |X|
—————————
| item1 -> item2 ->++|
| item3 ->… ++++++|
|++++++++++++++|
|++++++++++++++|
—————————-
*Items are placed in a flow pattern, one after another left to right and top
to bottom. (If the direction chosen is LEFT, other choices are available)

It is similar to the null layout, but the flow layout arranges the items for you, with a speficied horizontal and vertical gap, all defined within the 3 constructors allowed in this layout:

public FlowLayout();
-the default constructor, with default values supplied, they should be:
align = LEFT
hGap = 0
vGap = 0
*but you may want to check that this has stayed the same in updates to the JVM
public FlowLayout(int align);
-where only the alignment must be specified, and hGap and vGap are supplied as above.
public FlowLayout (int align, int hGap, int vGap);
-where you can supply all the variables to your design needs.

CODE Example:
import java.awt.*;
import javax.swing.*;
public class FlowLayoutManagerExample extends JFrame {

public FlowLayoutManagerExample (String title) {

super(title);
Container contentPane = getContentPane();

// Set the layoutManager
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));

// Add components using the layout manager
contentPane.add(new JButton(”one”));
contentPane.add(new JButton(”two”));
contentPane.add(new JButton(”three”));
public static void main(String args[]) {

FlowLayoutManagerExample frame = new FlowLayoutManagerExample(”Example”);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);

}

}

*NOTE: this frame’s buttons do not actually do anything, this is simply an example of using the layout manager
-try changing the fram.setResizable to true, and resize the frame.

Questions or Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

Previous recipe | Next recipe |
 
blog comments powered by Disqus