0% found this document useful (0 votes)
8 views

Java_GridBagLayout java

Java notes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java_GridBagLayout java

Java notes
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java GridBagLayout

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.

You can set the following GridBagConstraints instance variables:


o gridx, gridy: Specify the row and column at the upper left of the component. The
leftmost column has address gridx=0 and the top row has address gridy=0. Use
GridBagConstraints.RELATIVE (the default value) to specify that the component be
placed just to the right of (for gridx) or just below (for gridy) the component that was
added to the container just before this component was added. We recommend
specifying the gridx and gridy values for each component rather than just using
GridBagConstraints.RELATIVE; this tends to result in more predictable layouts.
o gridwidth, gridheight: Specify the number of columns (for gridwidth) or rows (for
gridheight) in the component’s display area. These constraints specify the number of
cells the component uses, not the number of pixels it uses. The default value is 1.
Use GridBagConstraints.REMAINDER to specify that the component be the last one
in its row (for gridwidth) or column (for gridheight). Use
GridBagConstraints.RELATIVE to specify that the component be the next to last
one in its row (for gridwidth) or column (for gridheight). We recommend specifying
the gridwidth and gridheight values for each component rather than just using
GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER; this tends
to result in more predictable layouts.
o fill:Used when the component’s display area is larger than the component’s
requested size to determine whether and how to resize the component. Valid values
(defined as GridBagConstraints constants) include NONE (the default),
HORIZONTAL (make the component wide enough to fill its display area
horizontally, but do not change its height), VERTICAL (make the component tall
enough to fill its display area vertically, but do not change its width), and BOTH
(make the component fill its display area entirely).
o ipadx, ipady :Specifies the internal padding: how much to add to the size of the
component. The default value is zero. The width of the component will be at least its
minimum width plus ipadx*2 pixels, since the padding applies to both sides of the
component. Similarly, the height of the component will be at least its minimum
height plus ipady*2 pixels.
o insets:Specifies the external padding of the component — the minimum amount of
space between the component and the edges of its display area. The value is
specified as an Insets object. By default, each component has no external padding.
o weightx, weighty: Specifying weights is an art that can have a significant impact on
the appearance of the components a GridBagLayout controls. Weights are used to
determine how to distribute space among columns (weightx) and among rows
(weighty); this is important for specifying resizing behavior.
Unless you specify at least one non-zero value for weightx or weighty, all the
components clump together in the center of their container. This is because when the
weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid
of cells and the edges of the container.

Instance variables to manipulate the GridBagLayout object Constraints


are:

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.

Insets(int top, int left, int bottom, int right)

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.*;

public class InsetsDemo extends Applet


{

public void init()


{
setBackground(Color.cyan);
setLayout(new BorderLayout());
add(new Button("RMI"),BorderLayout.NORTH);
add(new Button("SERVLET"),BorderLayout.EAST);
add(new Button("JDBC"),BorderLayout.SOUTH);
add(new Button("BEANS"),BorderLayout.WEST);
add(new Button("JAVA"),BorderLayout.CENTER);
}

public Insets getInsets()


{
return new Insets(10,20,10,20);
}
}
/*
<applet code=”InsetsDemo.class” width=300 height=200>
</applet>
*/

How to Execute this Example??

d:\> javac InsetsDemo.java


d:\> appletviewer InsetsDemo.java

Output:

You might also like