Layouts - Part 5 - FlowLayout

FlowLayout is the simplest of all layouts, and is the default LayoutManager of the JPanel.

In FlowLayout, widgets are placed one after another going left to right. This occurs until the end of the JPanel is reached, then a new row is started.

There are three constructors for the FlowLayout. The first constructor is the default. By default, the spacing between these widgets is about 5 pixels and the alignment is FlowLayout.CENTER.
The second constructor lets you set the alignment. You can use...

FlowLayout.LEADING
Picture of FlowLayout

FlowLayout.CENTER (default)
Picture of FlowLayout

or FlowLayout.TRAILING
Picture of FlowLayout

The spacing in the third constructor is pretty intuitive, it just increases the spacing between the widgets.
Here's the code for the examples of layouts shown above. All I've changed is line 12 where we instantiate the FlowLayout.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class FlowLayoutExample{

    public JPanel createContentPane (){
    
        JPanel totalGUI = new JPanel();
        
        // We create a JPanel with the FlowLayout.
        JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        mainPanel.setPreferredSize(new Dimension(200, 120));
        
        JPanel red = createSquareJPanel(Color.red, 50);
        JPanel blue = createSquareJPanel(Color.blue, 50);
        JPanel green = createSquareJPanel(Color.green, 50);
        JPanel orange = createSquareJPanel(Color.orange, 50);
        JPanel yellow = createSquareJPanel(Color.yellow, 50);
        
        mainPanel.add(red);
        mainPanel.add(blue);
        mainPanel.add(green);
        mainPanel.add(orange);
        mainPanel.add(yellow);

        totalGUI.add(mainPanel);
        totalGUI.setOpaque(true);
        return totalGUI;
    }

    // In this method, we create a square JPanel of a colour and set size
    // specified by the arguments.
    
    private JPanel createSquareJPanel(Color color, int size)
    {
        JPanel tempPanel = new JPanel();
        tempPanel.setBackground(color);
        tempPanel.setMinimumSize(new Dimension(size, size));
        tempPanel.setMaximumSize(new Dimension(size, size));
        tempPanel.setPreferredSize(new Dimension(size, size));
        return tempPanel;
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] FlowLayout [=]");

        FlowLayoutExample demo = new FlowLayoutExample();
        frame.setContentPane(demo.createContentPane());
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
This really is the simplest LayoutManager of all, and can be very handy if you just need something on a screen and spaced out a little.

Questions

Questions on the FlowLayout you really should get.

Question 1. Which of these alignments is false?

  • a) FlowLayout.LEADING
  • b) FlowLayout.CENTER
  • c) FlowLayout.FIRST




Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict