Java Swing How to - Set Component to adhere to bottom with GridBagLayout








Question

We would like to know how to set Component to adhere to bottom with GridBagLayout.

Answer

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
//from  ww w  . j av a 2s. c o  m
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main extends JFrame {

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

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.SOUTHEAST;
    c.weighty = 1;
    add(f, c);
  }

  public static void main(String... strings) {
    Main e = new Main();
    e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    e.pack();
    e.setLocationRelativeTo(null);
    e.setVisible(true);
  }

}