Thursday, July 8, 2010

Grid Bag Layout  

0 comments

The GridBag Layout manager is the most flexible and complex layout manager that the AWT provides. It places components in rows and columns allowing specific components by assigning weights to individual components in the gridbag layout.

When you specify the size and position of components,you also need to specify the constraint for each component.To specify constraint ,you need to set the variables in GridBagConstraints Object and specify theGridBagLayout manager object with the setConstraint() method, to associate the constraint with the component.

The GridBagLayout class has a single constructor that does not take any arguments.Since the position of each component in a layout is controlled by a GridBagLayout object and is determined by the currently setGridBagConstraints object,you need to create the GridBagConstraintsobject before the layout can be built.

GridBagLayout g=new GridBagLayout();
GridBagConstraints cons=new GridBagConstraints();




You can assign following values to GridBagConstraints attributes:-

anchor:-It is used when the component is smaller than its display area to determine where to place the compoenent.valid values are shown below:

GridBagConstraints.CENTER
GridBagConstraints.NORTH
GridBagConstraints.NORTHEAST
GridBagConstraints.EAST
GridBagConstraints.SOUTHEAST
GridBagConstraints.SOUTH
GridBagConstraints.SOUTHWEST
GridBagConstraints.WEST
GridBagConstraints.NORTHWEST

fill:- It is used when the display area of component is larger than the requested size of the component to determine whether to resize the component.valid values are:-
GridBagConstraints.NONE
GridBagConstraints.HORIZONTAL
GridBagConstraints.VERTICAL
GridBagConstraints.BOTH

gridwidth,gridheight:-It specifies number of columns or rows in display area of components the default value is 1,valid values are:
GridBagConstraints.REMAINDER
GridBagConstraints.RELATIVE

gridx,gridy:-They specify the row and column at the upper left of the components display area.The leftmost column has the address gridx=0 and topmost cell has the address gridy=0.

insets:- They specify the extenal padding of the component ,that is ,the minimum amount of space between the components and the edges of its display area.The value is specified as an Insets object .By default ,each component does not have external padding.The default padding value is insets(0,0,0,0).

ipadx,ipady:-They specify the internal padding to be added to the minimum size of the component.The default value is 0.The width of the component will be its minimum width plus ipadx*2 pixels.Similarly ,the height of the component will be its minimum height plus ipady*2 pixels.

weightx,weighty:- They determine whether the component stretch horizontally to fill the display area of the applet or vertically.The default value is 0 for both.



Example
Source code:-

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GridBagWindow extends JFrame {
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;

public GridBagWindow() {
JButton button;
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}

button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("2");
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("Button 3");
c.gridx = 2;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("Long-Named Button 4");
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("Button 5");
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.SOUTH; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
gridbag.setConstraints(button, c);
contentPane.add(button);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}

public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.inAnApplet = false;

window.setTitle("GridBagLayout");
window.pack();
window.setVisible(true);
}
}

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



0 comments: to “ Grid Bag Layout

Extension Factory Builder