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() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);//  ww w. j  a  va  2s .com
    c.weightx = 1.0;
    c.weighty = 1.0;
    makebutton("Button 1", gridbag, c);
    c.fill = GridBagConstraints.BOTH;
    makebutton("Button 2", gridbag, c);
}

From source file:Main.java

private JPanel createPanel() {
    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;/* w w  w  .j  a  v a  2s .  c o m*/
    c.gridy = 0;
    c.anchor = GridBagConstraints.BASELINE_LEADING;
    p.add(new JLabel("Loan amount"), c);

    c.gridx++;
    p.add(new JTextField(15), c);

    c.gridx++;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    p.add(new JLabel("AUD"), c);

    c.gridx = 0;
    c.gridy++;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    p.add(new JLabel("Loan term"), c);

    c.gridx++;
    p.add(new JTextField(15), c);

    c.gridx++;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    p.add(new JLabel("Years"), c);

    return p;
}

From source file:Main.java

public Main() {
    JPanel bigPanel = new JPanel(new GridBagLayout());
    JPanel panel_a = new JPanel();
    JButton btnA = new JButton("button a");
    panel_a.add(btnA);//w  ww .j a va  2  s  . c  o  m

    JPanel panel_b = new JPanel();
    JButton btnB = new JButton("button b");
    panel_b.add(btnB);

    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1.0;
    bigPanel.add(panel_a, c);
    bigPanel.add(panel_b, c);
    c.weighty = 1.0;

    bigPanel.add(new JPanel(), c);
    this.add(bigPanel);
}

From source file:Main.java

public void buildGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame();
    f.setResizable(false);/*from  www .  j  av  a 2 s.c om*/
    removeMinMaxClose(f);
    JPanel p = new JPanel(new GridBagLayout());
    JButton btn = new JButton("Exit");
    p.add(btn, new GridBagConstraints());
    f.getContentPane().add(p);
    f.setSize(400, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    btn.addActionListener(e -> System.exit(0));
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    JPanel upper = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    upper.setLayout(gridbag);/*from   w  w  w .j a  va  2 s  .  c  o m*/
    GridBagConstraints gbc = new GridBagConstraints();

    JButton toolbar1 = new JButton("toolbar1");
    JButton toolbar2 = new JButton("toolbar2");
    JButton toolbar3 = new JButton("toolbar3");

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.anchor = GridBagConstraints.WEST;
    upper.add(toolbar1, gbc);

    gbc.gridx = 1;
    gbc.anchor = GridBagConstraints.CENTER;
    upper.add(toolbar2, gbc);

    gbc.gridx = 2;
    gbc.anchor = GridBagConstraints.EAST;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    upper.add(toolbar3, gbc);

    add(upper, BorderLayout.NORTH);

    JPanel something = new JPanel();
    something.setBackground(Color.WHITE);
    something.setPreferredSize(new Dimension(600, 600));
    something.repaint();
    add(something, BorderLayout.CENTER);

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(500, 500));
    getContentPane().setLayout(new BorderLayout());
    JPanel panel = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(4, 4, 4, 4);
    panel.add(new JLabel("Label"), c);

    panel.add(new JTextField(20), c);

    c.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(new JButton("btn"), c);
    c.gridwidth = 1;//from  ww w .  j  a  v  a2 s  .co  m

    panel.add(new JLabel("Name"), c);

    panel.add(new JTextField(20), c);

    c.gridwidth = GridBagConstraints.REMAINDER;
    panel.add(new JButton("btn"), c);
    c.weighty = 1.0;
    panel.add(Box.createGlue(), c);

    add(panel, BorderLayout.LINE_START);

    pack();
    setVisible(true);
}

From source file:GridBagConstraintsSimplePanel.java

public GridBagConstraintsSimplePanel() {
    super();/* w  w w  .  j  a  v a  2s  . c  om*/
    GridBagConstraints constraints = new GridBagConstraints();
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    constraints.anchor = GridBagConstraints.WEST;

    constraints.gridy = 0;
    JLabel label = new JLabel("First name:");
    add(label, constraints);

    JTextField tf = new JTextField(8);
    add(tf, constraints);
    label = new JLabel("Last name:");
    add(label, constraints);

    tf = new JTextField(8);
    add(tf, constraints);

    constraints.gridy = 1;
    label = new JLabel("Address:");
    add(label, constraints);

    tf = new JTextField(10);
    add(tf, constraints);
}

From source file:Main.java

public TestPane() {
    JLabel longText = new JLabel("Long Long Text");
    JLabel shortText = new JLabel("Short");
    JLabel medText = new JLabel("Medium");

    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;//from   w  ww  .j a v  a 2s .com
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.EAST;
    add(longText, gbc);
    addFields(gbc);

    gbc.gridx = 0;
    gbc.gridy++;
    gbc.anchor = GridBagConstraints.EAST;
    add(shortText, gbc);
    addFields(gbc);

    gbc.gridx = 0;
    gbc.gridy++;
    gbc.anchor = GridBagConstraints.EAST;
    add(medText, gbc);
    addFields(gbc);
}

From source file:Main.java

public GridBagLayoutPanel() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(gridbag);//from   w  w w. ja va 2 s.  com

    constraints.weightx = constraints.weighty = 10.0;
    constraints.fill = constraints.NONE;
    constraints.ipadx = 30;
    constraints.ipady = 10;
    addButton("Press", constraints, gridbag);

    constraints.weightx = 5.0;
    constraints.fill = constraints.BOTH;
    constraints.ipadx = constraints.ipady = 0;
    constraints.insets = new Insets(10, 30, 10, 20);
    constraints.gridwidth = constraints.RELATIVE;
    constraints.gridheight = 2;
    addButton("GO", constraints, gridbag);

    constraints.insets = new Insets(0, 0, 0, 0);
    constraints.gridx = 0;
    constraints.fill = constraints.NONE;
    constraints.ipadx = 30;
    constraints.ipady = 10;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    addButton("Push", constraints, gridbag);

}

From source file:Main.java

/**
 * @param panel/*  ww w.  j  a va  2 s  .  c  o m*/
 * @return pre-configured constraints
 */
public static GridBagConstraints initPanel(JPanel panel) {
    panel.setLayout(new GridBagLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    GridBagConstraints gb = new GridBagConstraints();
    gb.gridx = 0;
    gb.gridy = 0;
    gb.insets = new Insets(2, 2, 2, 2);
    return gb;
}