Example usage for java.awt GridBagConstraints GridBagConstraints

List of usage examples for java.awt GridBagConstraints GridBagConstraints

Introduction

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

Prototype

public GridBagConstraints() 

Source Link

Document

Creates a GridBagConstraint object with all of its fields set to their default value.

Usage

From source file:Main.java

public Main() {
    JTextField f = new JTextField(20);

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.SOUTHEAST;
    c.weighty = 1;// w w  w . j av  a  2  s .  c  om
    add(f, c);
}

From source file:Main.java

public Main() {
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    getContentPane().setLayout(layout);//from   w  w  w . j a v  a2 s.c om
    constraints.anchor = GridBagConstraints.WEST;
    JLabel l1 = new JLabel("First Name:");
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 5, 5, 5);
    layout.setConstraints(l1, constraints);
    getContentPane().add(l1);

    JTextField t1 = new JTextField();
    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.weightx = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(5, 5, 5, 5);
    layout.setConstraints(t1, constraints);
    getContentPane().add(t1);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(700, 500);
}

From source file:Main.java

public Main() {
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    JPanel panel1 = new JPanel();
    Border eBorder = BorderFactory.createEtchedBorder();
    panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
    gbc.gridx = gbc.gridy = 0;/*from   www . j  av a2 s  .c  o m*/
    gbc.gridwidth = gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = gbc.weighty = 70;
    getContentPane().add(panel1, gbc);
    JPanel panel2 = new JPanel();
    panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
    gbc.gridy = 1;
    gbc.weightx = 30;
    gbc.weighty = 30;
    gbc.insets = new Insets(2, 2, 2, 2);
    getContentPane().add(panel2, gbc);
    pack();
}

From source file:Main.java

public Main() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    JPanel panel1 = new JPanel();
    Border eBorder = BorderFactory.createEtchedBorder();

    panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
    gbc.gridx = gbc.gridy = 0;/*from w  w w  .ja  va 2 s  . co m*/
    gbc.gridwidth = gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weightx = gbc.weighty = 70;
    add(panel1, gbc);

    JPanel panel2 = new JPanel();
    panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
    gbc.gridy = 1;
    gbc.weightx = gbc.weighty = 30;
    gbc.insets = new Insets(2, 2, 2, 2);
    add(panel2, gbc);

    JPanel panel3 = new JPanel();
    panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 2;
    gbc.weightx = 20;
    gbc.insets = new Insets(2, 2, 2, 2);
    add(panel3, gbc);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

From source file:Main.java

public static void addComponent(final Container container, final GridBagLayout gbl, final Component c,
        final int x, final int y, final int width, final int height, final double weightx, final double weighty,
        final int anchor, final int fill, final Insets insets) {
    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = fill;/*from   w  w  w .j  a  v a  2  s  . co m*/
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    gbc.weightx = weightx;
    gbc.weighty = weighty;
    gbc.anchor = anchor;
    gbc.insets = insets;
    gbl.setConstraints(c, gbc);
    container.add(c);
}

From source file:Main.java

public static JPanel createKV(final Component key, final Component value, final int keyWidth,
        final boolean fill) {
    initComponentHeight(key, value);/* w  w w  .ja  va2 s  . c  o  m*/
    if (keyWidth > 0) {
        key.setPreferredSize(new Dimension(keyWidth, key.getPreferredSize().height));
    }
    final JPanel jp = new JPanel(new GridBagLayout());
    final GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(0, 0, 0, 4);
    jp.add(key, gbc);
    gbc.gridx = 1;
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.weightx = 1.0;
    if (fill) {
        gbc.fill = GridBagConstraints.HORIZONTAL;
    }
    jp.add(value, gbc);
    return jp;
}

From source file:Main.java

Main() {
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;/* w  w  w  . j  a  va 2 s . c o  m*/

    this.add(new Button("Resizable"), c);
    c = new GridBagConstraints();
    c.weightx = 0.0;

    this.add(new Button("Not Resizable"));
    this.pack();
}

From source file:Main.java

/**
 * Gridbaglayout helper method.// ww  w .j a va  2s .  c  o  m
 * @param container container that component gets added on.
 * @param component component to be added and set constraints for.
 * @param gridx GridBagConstraints argument.
 * @param gridy GridBagConstraints argument.
 * @param gridwidth GridBagConstraints argument.
 * @param gridheight GridBagConstraints argument.
 * @param weightx GridBagConstraints argument.
 * @param weighty GridBagConstraints argument.
 * @param anchor GridBagConstraints argument.
 * @param fill GridBagConstraints argument.
 * @param insets GridBagConstraints argument.
 * @param ipadx GridBagConstraints argument.
 * @param ipady GridBagConstraints argument.
 */
public static void addWithAllGridBagConstraints(final Container container, final Component component,
        final int gridx, final int gridy, final int gridwidth, final int gridheight, final double weightx,
        final double weighty, final int anchor, final int fill, final Insets insets, final int ipadx,
        final int ipady) {
    GridBagConstraints gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = gridx;
    gridBagConstraints.gridy = gridy;
    gridBagConstraints.gridheight = gridheight;
    gridBagConstraints.gridwidth = gridwidth;
    gridBagConstraints.weightx = weightx;
    gridBagConstraints.weighty = weighty;
    gridBagConstraints.anchor = anchor;
    gridBagConstraints.fill = fill;
    gridBagConstraints.insets = insets;
    gridBagConstraints.ipadx = ipadx;
    gridBagConstraints.ipady = ipady;
    container.add(component, gridBagConstraints);
}

From source file:Main.java

public Main() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    this.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.anchor = GridBagConstraints.NORTH;
    gbc.weighty = 0.0;//from   w ww.  j  a va  2  s. c o  m

    JPanel one = new JPanel();
    one.setPreferredSize(new Dimension(200, 200));
    one.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    JPanel two = new JPanel();
    two.setPreferredSize(new Dimension(200, 200));
    two.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    this.add(one, gbc);

    gbc.gridy = 1;
    gbc.weighty = 0.0;
    gbc.fill = GridBagConstraints.VERTICAL;

    this.add(two, gbc);

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

From source file:Main.java

Main() {

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(500, 500);/*from   w  w w  . ja v a2 s  . c o m*/

    JPanel panel1 = new JPanel(new GridBagLayout());
    JButton b1 = new JButton("button 1"), b2 = new JButton("button 2");
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridwidth = 1;
    panel1.add(b1, gbc);
    gbc.gridx = 2;
    gbc.gridwidth = 2;
    gbc.fill = gbc.HORIZONTAL; // set fill property to HORIZONTAL
    gbc.weightx = 2.0;
    panel1.add(b2, gbc); // While adding button also add it with gbc
    add(panel1);
    setVisible(true);
}