Example usage for java.awt GridBagLayout GridBagLayout

List of usage examples for java.awt GridBagLayout GridBagLayout

Introduction

In this page you can find the example usage for java.awt GridBagLayout GridBagLayout.

Prototype

public GridBagLayout() 

Source Link

Document

Creates a grid bag layout manager.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);//w w w .j a  v  a 2  s  . co  m

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;

    JButton component = new JButton("a");

    gbl.setConstraints(component, gbc);

    container.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;//from w  ww.j a  va  2 s.  c  om
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);//from w  ww  .j a  va  2 s.  com

    // Place a component at cell location (1,1)
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;

    JButton component = new JButton("a");

    // Associate the gridbag constraints with the component
    gbl.setConstraints(component, gbc);

    container.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);//  w  ww .  j  a va 2s .c om

    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);

    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String arg[]) {
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());

    JPanel gb = new JPanel(new GridBagLayout());
    JLabel content = new JLabel("Some text");

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weighty = 1;/* w  w w.j a va 2  s  .  co m*/

    gb.add(content, gbc); // gbc is containing the GridBagConstraints
    frame.add(gb, BorderLayout.CENTER);

    frame.setVisible(true);
}

From source file:RelativeXY.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;//from w w w .  j  a v  a  2  s .  c  o m
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, first column"), gbc);
    pane.add(new JButton("Second row"), gbc);
    pane.add(new JButton("Third row"), gbc);
    gbc.gridx = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, second column"), gbc);
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:RelativeX.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridy = 0;//from   w w w .j a va 2 s . com
    pane.add(new JButton("First row"), gbc);
    gbc.gridx = GridBagConstraints.RELATIVE;
    gbc.gridy = 1;
    pane.add(new JButton("Second row, first column"), gbc);
    pane.add(new JButton("Second row, second column"), gbc);
    pane.add(new JButton("Second row, third column"), gbc);
    gbc.gridy = 2;
    pane.add(new JButton("Third row"), gbc);
    f.setSize(600, 300);
    f.setVisible(true);
}

From source file:RelativeY.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;//from w w  w .j  a  va  2  s .c o  m
    pane.add(new JButton("First column"), gbc);
    gbc.gridx = 1;
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("Second column, first row"), gbc);
    pane.add(new JButton("Second column, second row"), gbc);
    pane.add(new JButton("Second column, third row"), gbc);
    gbc.gridx = 2;
    pane.add(new JButton("Third column"), gbc);
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:GridBagLayoutFill.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;//from w  w  w  . j av a 2  s  .  c om
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("This button's preferred width " + "is large because its text is long"), gbc);
    pane.add(new JButton("Small centered button"), gbc);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    pane.add(new JButton("Expands to fill column width"), gbc);
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);/*  w  w  w  .  j a v a2 s  .co  m*/

    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);

    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);

    gbl.layoutContainer(container);

    int[][] dim = gbl.getLayoutDimensions();
    int cols = dim[0].length;
    int rows = dim[1].length;

    System.out.println(cols);
    System.out.println(rows);
}