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 Crazy on the Outside Online Free
  • Watch It’s Complicated Full Video Online
  • Watch Leap Year Free Stream Online
  • Watch Manchester United vs Birmingham Live Stream
  • Watch Avatar Online Stream
  • Watch Nine Online Video
  • Watch Crazy Heart Full Stream
  • Modern Family Season 1 Episode 11 | Watch Modern Family s01e11 Up All Night Full Episode
  • Burnley vs Arsenal EPL | Watch Burnley vs Arsenal Live Stream
  • Watch The Princess and the Frog Online Free

Warning: include(adbottom.html) [function.include]: failed to open stream: No such file or directory in /var/www/vhosts/iwebie.com/httpdocs/wp-content/themes/iWebie/rightsidebar.php on line 20

Warning: include() [function.include]: Failed opening 'adbottom.html' for inclusion (include_path='.:/opt/lsws/lsphp5/lib/php') in /var/www/vhosts/iwebie.com/httpdocs/wp-content/themes/iWebie/rightsidebar.php on line 20
Archives

  • January 2010
  • December 2009
  • 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