Java Swing: GridBagLayout (Video Tutorial Part 4)

Conquer your fears of this, the most fearsome of the layout managers in Java, but also the most flexible and the most useful. Many Java programmers pretty much just use this one layout manager for everything. I explain step by step how to layout your Swing controls and what the various GridBagConstraints fields do.

After starting the video, click the maximise button to make it fullscreen so you can see the code!




Code For This Tutorial



Main app:

 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
public class App {
 
    public static void main(String[] args) {
 
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new MainFrame("Hello World Swing!");
                frame.setSize(500, 400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}
 

Window class:

 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class MainFrame extends JFrame {
    
    private DetailsPanel detailsPanel;
    
    public MainFrame(String title) {
        super(title);
        
        // Set layout manager
        setLayout(new BorderLayout());
        
        // Create Swing component
        final JTextArea textArea = new JTextArea();
        JButton button = new JButton("Click me!");
        
        detailsPanel = new DetailsPanel();
        
        // Add Swing components to content pane
        Container c = getContentPane();
        
        c.add(textArea, BorderLayout.CENTER);
        c.add(button, BorderLayout.SOUTH);
        c.add(detailsPanel, BorderLayout.WEST);
        
        // Add behaviour
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append("Hello
");
            }
            
        });
    }
}



 

Panel class (we'll complete this in the next tutorial):

 

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class DetailsPanel extends JPanel {
    public DetailsPanel() {
        Dimension size = getPreferredSize();
        size.width = 250;
        setPreferredSize(size);
        
        setBorder(BorderFactory.createTitledBorder("Personal Details"));
        
        JLabel nameLabel = new JLabel("Name: ");
        JLabel occupationLabel = new JLabel("Occupation: ");
        
        JTextField nameField = new JTextField(10);
        JTextField occupationField = new JTextField(10);
        
        JButton addBtn = new JButton("Add");
        
        setLayout(new GridBagLayout());
        
        GridBagConstraints gc = new GridBagConstraints();
        
        //// First column /////////////////////////
        
        gc.anchor = GridBagConstraints.LINE_END;
        gc.weightx = 0.5;
        gc.weighty = 0.5;
        
        gc.gridx = 0;
        gc.gridy = 0;
        add(nameLabel, gc);
        
        gc.gridx = 0;
        gc.gridy = 1;
        add(occupationLabel, gc);
        
        //// Second column
        gc.anchor = GridBagConstraints.LINE_START;
        
        gc.gridx = 1;
        gc.gridy = 0;
        add(nameField, gc);
        
        gc.gridx = 1;
        gc.gridy = 1;
        add(occupationField, gc);
        
        // Final row
        gc.weighty = 10;
        
        gc.anchor = GridBagConstraints.FIRST_LINE_START;
        gc.gridx = 1;
        gc.gridy = 2;
        add(addBtn, gc);
    }
}