Java_GridBagLayout java
Java_GridBagLayout java
The Java GridBagLayout class is used to align components vertically, horizontally or along their
baseline.
The components may not be of same size. Each GridBagLayout object maintains a dynamic,
rectangular grid of cells. Each component occupies one or more cells known as its display area.
Each component associates an instance of GridBagConstraints. With the help of constraints object
we arrange component's display area on the grid. The GridBagLayout manages each component's
minimum and preferred sizes in order to determine component's size.
Variable Role
gridx and These contain the coordinates of the origin of the
gridy grid. They allow a at a specific position of a
component positioning. By default, they have
GrigBagConstraint.RELATIVE value which indicates
that a component can be stored to the right of
previous
gridwidth, Define how many cells will occupy component
gridheight (height and width). by The default is 1. The
indication is relative to the other components of the
line or the column. The
GridBagConstraints.REMAINDER value specifies
that the next component inserted will be the last of
the line or the current column. the value
GridBagConstraints.RELATIVE up the component
after the last component of a row or column.
fill Defines the fate of a component smaller than the grid
cell.
GridBagConstraints.NONE retains the original size:
Default
GridBagConstraints.HORIZONTAL expanded
horizontally
GridBagConstraints.VERTICAL
GridBagConstraints.BOTH expanded vertically
expanded to the dimensions of the cell
ipadx, ipady Used to define the horizontal and vertical expansion
of components. not works if expansion is required by
fill. The default value is (0,0).
anchor When a component is smaller than the cell in which
it is inserted, it can be positioned using this variable
to define the side from which the control should be
aligned within the cell. Possible variables NORTH,
NORTHWEST, NORTHEAST, SOUTH, SOUTHWEST,
SOUTHEAST, WEST and EAST
weightx, Used to define the distribution of space in case of
weighty change of dimension
import java.awt.*;
class GridBagLayoutExample extends Frame
{
GridBagLayoutExample()
{
Label lblName = new Label("Name");
TextField txtName =new TextField(10);
Label lblcomments = new Label("Comments");
TextArea TAreaComments=new TextArea(6,15);
Button btnSubmit = new Button("Submit");
setLayout(new GridBagLayout());
GridBagConstraints gc =new GridBagConstraints();
add(lblName,gc,0,0,1,1,0,0);
add(txtName,gc,1,0,1,1,0,20);
add(lblcomments,gc,0,1,1,1,0,0);
add(TAreaComments,gc,1,1,1,1,0,60);
add(btnSubmit,gc,0,2,2,1,0,20);
}
void add(Component comp,GridBagConstraints gc,int x,int y,int w,int
h,int wx,int wy)
{
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight= h;
gc.weightx = wx;
gc.weighty = wy;
add(comp,gc);
}
}
class GridBagLayoutJavaExample
{
public static void main(String args[])
{
GridBagLayoutExample frame = new GridBagLayoutExample();
frame.setTitle("GridBagLayout in Java Example");
frame.setSize(300,200);
frame.setVisible(true);
}
}
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample extends JFrame{
public static void main(String[] args) {
GridBagLayoutExample a = new GridBagLayoutExample();
}
public GridBagLayoutExample() {
GridBagLayoutgrid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
this.add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
this.add(new Button("Button Three"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
this.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Output:
Insets
If you want to leave a small space between the container that holds your components and the window that
contains it. To do this, override the getInsets() method that is defined by the container.
The values passed in top, left, bottom and right specify the amount of space between the container and its
enclosing window.
Example:
import java.awt.*;
import java.applet.*;
Output: