Layouts - Part 6

GridLayout

The GridLayout is quite a simple layout. You specify the number of rows and columns you wish to have on the grid, then as the widgets are added they fill the grid from left to right. Each cell is of equal size, and the grid layout sizes the cells to fill the entire space of the Panel.
The widgets within the cell are stretched to fill the entire cell. There are two constructors for the GridLayout. In the first constructor, you use integers as arguments to specify the number of rows and columns.
By default, there is no space between the cells so if you wish to have space you should use the second constructor.

Let's have a look at some pictures of example layouts followed by the code used. I've tweaked the last example to contain 10 coloured boxes of 50px by 50px.

GridLayout(1, 10);
Picture of GridLayout

GridLayout(3, 4);
Picture of GridLayout

GridLayout(5, 2);
Picture of GridLayout

GridLayout(3, 4, 10, 30);
Picture of GridLayout

Here's the code used for those examples. Bear in mind, all I change between pictures is Line 12.
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
68
69
70
71
72
73
74
75
76
import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class GridLayoutExample{

    public JPanel createContentPane (){
        
        JPanel totalGUI = new JPanel();
        
        // We create a JPanel with the GridLayout.
        JPanel mainPanel = new JPanel(new GridLayout(3, 4, 10, 30));
        
        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);
        JPanel pink = createSquareJPanel(Color.pink, 50);
        JPanel cyan = createSquareJPanel(Color.cyan, 50);
        JPanel gray = createSquareJPanel(Color.gray, 50);
        JPanel black = createSquareJPanel(Color.black, 50);
        JPanel magenta = createSquareJPanel(Color.magenta, 50);
      
        mainPanel.add(red);
        mainPanel.add(blue);
        mainPanel.add(green);
        mainPanel.add(orange);
        mainPanel.add(yellow);
        mainPanel.add(pink);
        mainPanel.add(cyan);
        mainPanel.add(gray);
        mainPanel.add(black);
        mainPanel.add(magenta);

        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("[=] GridLayout [=]");

        GridLayoutExample demo = new GridLayoutExample();
        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();
            }
        });
    }
}
GridLayout is very handy for displaying thumbnails of pictures, or anything of equal sizes that you need spaced evenly over an area.

Questions

Questions on the FlowLayout you really should get.

Question 1. How do you instantiate a GridLayout?

  • a) .setLayout(new GridLayout(int horizontal, int verticalspace));
  • b) .setLayout(new GridLayout(int rows, int columns));
  • c) .setLayout(new GridLayout(int columns, int rows));




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