Specifying gridx and gridy as RELATIVE with gridwidth as REMAINDER - Java Swing

Java examples for Swing:GridBagLayout

Description

Specifying gridx and gridy as RELATIVE with gridwidth as REMAINDER

Demo Code

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button 2");
    JButton b3 = new JButton("Button 3");

    gbc.gridx = 0;//www. ja  v a2s .c om
    gbc.gridy = 0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;// Last component in the row
    frame.add(b1, gbc);

    gbc.gridx = GridBagConstraints.RELATIVE;
    gbc.gridy = GridBagConstraints.RELATIVE;
    gbc.gridwidth = 1; // Reset to the default value
    frame.add(b2, gbc);

    gbc.gridx = GridBagConstraints.RELATIVE;
    gbc.gridy = 1;
    frame.add(b3, gbc);

    frame.add(b3, gbc);
    frame.pack();
    frame.setVisible(true);
  }
}

Related Tutorials