iwebie
HOME MOVIES TV SHOWS SPORTS GENERAL
 RSSPRIVACY
 

Java Swing GridBagLayout Example
Posted by admin on July 15th, 2008

Watch over 100,000 movies and TV shows on your PC

Java Swing GridBagLayout is a Swing layout manager that is difficult to use and needs good practice to master it. GridBagLayout lay’s out components vertically and horizontally within a grid of rows and columns that can be of different sizes. To understand GridBagLayout, its essential to know what a GridBagConstraint is ?

GridBagConstraints are used to specify how a component must be positioned within the display area of your panel. It gives you complete control to set various parameters like coordinate positions (gridx & gridy), width, height, (gridwidth & gridheight) internal padding (ipadx & ipady), external padding (insets), anchor, weightx and weighty.

Below is a Swing GridBagLayout Example that demonstrates

  • How to work with the GridBagLayout and GridBagConstraints classes.
  • How to create and set a gridbag layout
  • how to set gridbag constraints on each component that is added to the layout.

[sourcecode language='java']
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class JavaGridNagLayoutExample {
private JTextField jtfName;
private JTextField jtfAge;
private JTextArea jtaAddress;
private JTextField jtfState;
private JTextField jtfCity;
private JRadioButton jrbMale;
private JRadioButton jrbFemale;
private JButton jbtSubmit;

public void addComponentsToPane(JFrame frame) {
Container pane = frame.getContentPane();
ButtonGroup group = new ButtonGroup();
group.add(jrbMale);
group.add(jrbFemale);

GridBagConstraints gBC = new GridBagConstraints();
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.insets = new Insets(5, 5, 0, 5);

JLabel jlbName = new JLabel(”Name : “);
gBC.gridx = 0;
gBC.gridy = 0;
pane.add(jlbName, gBC);

jtfName = new JTextField(20);
gBC.gridx = 1;
gBC.gridy = 0;
gBC.gridwidth = 2;
pane.add(jtfName, gBC);

JLabel jlbAge = new JLabel(”Age : “);
gBC.gridx = 0;
gBC.gridy = 1;
gBC.gridwidth = 1;
pane.add(jlbAge, gBC);

jtfAge = new JTextField(5);
gBC.gridx = 1;
gBC.gridy = 1;
gBC.fill = GridBagConstraints.NONE;
gBC.anchor = GridBagConstraints.WEST;
gBC.gridwidth = 2;
pane.add(jtfAge, gBC);

gBC.fill = GridBagConstraints.HORIZONTAL;

JLabel jlbSex = new JLabel(”Sex : “);
gBC.gridx = 0;
gBC.gridy = 2;
gBC.gridwidth = 1;
pane.add(jlbSex, gBC);

jrbMale = new JRadioButton(”Male”);
gBC.gridx = 1;
gBC.gridy = 2;
pane.add(jrbMale, gBC);

jrbFemale = new JRadioButton(”Female”);
gBC.gridx = 2;
gBC.gridy = 2;
pane.add(jrbFemale, gBC);

JLabel jlbAddress = new JLabel(”Address : “);
gBC.gridx = 0;
gBC.gridy = 3;
pane.add(jlbAddress, gBC);

jtaAddress = new JTextArea(5, 10);
gBC.gridx = 1;
gBC.gridy = 3;
gBC.gridwidth = 2;
pane.add(jtaAddress, gBC);

JLabel jlbCity = new JLabel(”City : “);
gBC.gridx = 0;
gBC.gridy = 4;
gBC.gridwidth = 1;
pane.add(jlbCity, gBC);

jtfCity = new JTextField(10);
gBC.gridx = 1;
gBC.gridy = 4;
gBC.gridwidth = 2;
pane.add(jtfCity, gBC);

gBC.gridwidth = 1;

JLabel jlbState = new JLabel(”State : “);
gBC.gridx = 3;
gBC.gridy = 4;
gBC.insets.right = 0;
pane.add(jlbState, gBC);

gBC.insets.right = 5;

jtfState = new JTextField(10);
gBC.gridx = 4;
gBC.gridy = 4;
pane.add(jtfState, gBC);

jbtSubmit = new JButton(”Submit”);
gBC.gridx = 4;
gBC.gridy = 5;
pane.add(jbtSubmit, gBC);
}

public static void main(String[] args) {
JavaGridNagLayoutExample test = new JavaGridNagLayoutExample();
JFrame frame = new JFrame(”GridBagLayout Demo”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
test.addComponentsToPane(frame);
frame.pack();
frame.setVisible(true);
}
}
[/sourcecode]

An advantage of the GridBagLayout over GridLayout is that we can layout out components in rows and columns of different sizes.

Check GridBagLayout Class Related API

java.awt.GridBagLayout Class
java.awt.GridBagConstraints Class

You may also want to check out How to Use GridBagLayout  tutorial from the Sun Java Site .

Bookmark Article
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Propeller
3 Responses to “Java Swing GridBagLayout Example”
  1. Renjith Says: June 17th, 2009 at 6:06 pm

    Need to show the output sample also……

  2. Philemon Kasyoka Says: June 30th, 2009 at 7:11 pm

    Nice work, but i would be better if you set the components to have a fix size or scrollable,they expand the whole form if you enter too much data

  3. john wanyonyi krarmoja Says: September 19th, 2009 at 4:52 am

    that ok

Leave a Reply

Most Popular

Watch over 100,000 movies and TV shows on your PC
Recent Posts

  • Watch Dexter Season 4 Episode 9 | Dexter Hungry Man Online Video
  • Californication Season 3 Episode 9 | Watch Mr. Bad Example Full Episode
  • Watch The Amazing Race Season 15 Episode 9 | The Amazing Race s15e09 Full Video
  • English Premier League | Watch Aston Villa vs Burnley Streaming Live
  • Stargate Universe Season 1 Episode 9 | Watch Stargate Universe s01e09 Life Video Online
  • Smallville Season 9 Episode 9 | Watch Smallville s09e09 Pandora Online Free
  • Monk Season 8 Episode 14 | Watch Mr. Monk and the Badge Streaming Video
  • Community Episode 10 | Watch Community s01e10 Environmental Science Stream Online
  • Watch It’s Always Sunny in Philadelphia Season 5 Episode 10 Full Episode
  • Survivor: Samoa Episode 10 | Watch Survivor s19e10 The Day of Reckoning Free Online
Archives

  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
Powered by WordPress