Below is the source code to perform grid bag layout.
GridBagLayoutDemo.java
[java]package swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public GridBagLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
GridBagLayoutDemo swingLayoutDemo = new GridBagLayoutDemo();
swingLayoutDemo.showGridBagLayoutDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("GridBagLayout");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showGridBagLayoutDemo(){
headerLabel.setText("Layout in action: GridBagLayout");
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JButton("Button 1"),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(new JButton("Button 2"),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JButton("Button 3"),gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(new JButton("Button 4"),gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(new JButton("Button 5"),gbc);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
[/java]
Create a class and import all the packages to built GriBagLayout.
[java]package swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;[/java]
Create a class and include required frames, labels, panes and create a constructor also.
[java]public class GridBagLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public GridBagLayoutDemo(){
prepareGUI();
}[/java]
Write main method and create a object to the class and use that object.
[java]public static void main(String[] args){
GridBagLayoutDemo swingLayoutDemo = new GridBagLayoutDemo();
swingLayoutDemo.showGridBagLayoutDemo();[/java]
Set the title and size of the frame and set position of header label and status label.
[java]mainFrame = new JFrame("GridBagLayout");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);[java]
Add WindowListener and WindowAdapter to the main frame and write the function to the frame.
[java] mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);[/java]
Add header label, status label, control panel into the frame.
[java]mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true); [/java]
Add the text into the header label and color to the panel.
[java]private void showGridBagLayoutDemo(){
headerLabel.setText("Layout in action: GridBagLayout");
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);[/java]
Output:
Output will be as follows. Where components are not in similar size, in GridLayout components will be in rectangle grid shape.