Won Contests Let Us Talk Mail Me Light of Knowledge


M astering Java Swing - Part 1 : Setting Layouts
Java Swing is One of most Difficult to Digest and Best to use Topics i ever came across

      Swing provides many standard GUI components such as buttons, lists, menus, and text areas, which you combine to create your program's GUI. It also includes containers such as windows and tool bars.

      The Swing package was first available as an add-on to JDK 1.1. Prior to the introduction of the Swing package, the Abstract Window Toolkit (AWT) components provided all the UI components in the JDK 1.0 and 1.1 platforms. Although the Java 2 Platform still supports the AWT components, we strongly encourage you to use Swing components instead. You can identify Swing components because their names start with J. The AWT button class, for example, is named Button, whereas the Swing button class is named JButton. In addition, the AWT components are in the java.awt package, whereas the Swing components are in the javax.swing package.

      As a rule, programs should not use "heavyweight" AWT components alongside Swing components. Heavyweight components include all the ready-to-use AWT components, such as Menu and ScrollPane, and all components that inherit from the AWT Canvas and Panel classes. When Swing components (and all other "lightweight" components) overlap with heavyweight components, the heavyweight component is always painted on top.

The way u need to learn Swing is :

1) Master Component Placements such as TextBox, Label etc in Flow Layout [Simple Layout]
2) Master Component Placements in GridBag Layout [Complex Layout]
3) Understand Working with Click and Other Events
4) Working with Swing Tables, Tabbed Panes, Tree etc..
5) Database Connectivity
6) Planning and Building an Complete Application


DOWNLOAD CODE: swing1.zip

On your mark ......, Get Set..........., Go!
We start with opening our favorite Editor (mine is HomeSite 5) , copy n paste this code below and name it as HelloWorldSwing.java and save this code in C:\javacodes directory

Lines in Gray are the necessary import and class declaration
Lines in red are required to build a small frame with min, max and close buttons
Lines in Green are required to Exit the Swing Application on clicking the close button

import javax.swing.*;        

public class HelloWorldSwing 
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("HelloWorldSwing");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
c:\>md swingcodes
c:\>cd swingcodes
c:\javacodes>set classpath=
c:\javacodes>javac HelloWorldSwing.java
c:\javacodes>java HelloWorldSwing
You can see a small frame somewhere on the top of your screen with minimise, maximise and close button, with HelloWorldSwing Title Embedded with it, we will add a small text [Label] inside it , Concentrate on Lines in Blue

import javax.swing.*;        

public class HelloWorldSwing 
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("HelloWorldSwing");
		
        final JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
Recompile and Run the code again the output will be as
Setting the Layout Manager
Since we are planning to build up a fairly big Application its always good to start of with a fine Swing Skeleleton System,

Requirements Are :
1) Min, Max, Close Button
2) Display Application Title, Set Height and Width of the Application
3) a Container which holds many panels and panel which can hold many components such as Label and TextBox
4) Close the Application when close Button is Clicked
5) Flowlayout is easiest layout like wriing on a paper top right to topleft and after that start in new line

//Requirements and Imports HelloWorldSwing.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HelloWorldSwing extends JFrame
{
 //Main starts the Application and Initialises the Constructor
 public static void main(String args[])
 {
   HelloWorldSwing app = new HelloWorldSwing();
 }
 //Constructor Defines the Frame Title and calls Setup Method
 public HelloWorldSwing() 
 {
  super("My Simple Application"); 
  setup();
  pack();  		 
  show();
 }
 //Setup Method is responsible for Creating Panel, Defining Size, adding Componets to the Panel
 public void setup()
 {
  Container contentPane = getContentPane();
  contentPane.setLayout(new FlowLayout());
  JPanel jpanel1 = new JPanel();
  jpanel1 = new JPanel()
  {
   public Dimension getPreferredSize()
   {
    Dimension size = super.getPreferredSize();
    size.width = 300;	
    size.height = 100;	
    return size;
  }
 };	
 JLabel label1 = new JLabel("My Label");
 jpanel1.add(label1);	
 JTextField textbox1 = new JTextField("My TextBox",15);
 jpanel1.add(textbox1);		
 contentPane.add(jpanel1);
 addWindowListener(new WindowEventHandler());

}
//WindowEventHandler is responside for Closing the Application when close is clicked
class WindowEventHandler extends WindowAdapter 
{
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
}	
}
Recompile and Run the code again the output will be as
setting Borders, Displaying Image

//Requirements and Imports HelloWorldSwing.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HelloWorldSwing extends JFrame
{
 //Main starts the Application and Initialises the Constructor
 public static void main(String args[])
 {
   HelloWorldSwing app = new HelloWorldSwing();
 }
 //Constructor Defines the Frame Title and calls Setup Method
 public HelloWorldSwing() 
 {
  super("My Simple Application"); 
  setup();
  pack();  		 
  show();
 }
 //Setup Method is responsible for Creating Panel, Defining Size, adding Componets to the Panel
 public void setup()
 {
  Container contentPane = getContentPane();
  contentPane.setLayout(new FlowLayout());
  JPanel jpanel1 = new JPanel();
  jpanel1 = new JPanel()
  {
   public Dimension getPreferredSize()
   {
     Dimension size = super.getPreferredSize();				
     size.width = 300;	
     size.height = 100;	
     return size;
   }
 };	
 jpanel1.setBorder
 (BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"My Border 1"));
 JLabel label1 = new JLabel("My Label");
 jpanel1.add(label1);	
 JTextField textbox1 = new JTextField("My TextBox",15);
 jpanel1.add(textbox1);	
 JLabel label2 = new JLabel(new ImageIcon("think.gif"));
 jpanel1.add(label2);	
 contentPane.add(jpanel1);

 addWindowListener(new WindowEventHandler()); 

 }
 //WindowEventHandler is responside for Closing the Application when close is clicked
 class WindowEventHandler extends WindowAdapter 
 {
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
 }	
 }
 
Recompile and Run the code again the output will be as
Adding 2 More Panels which holds Radio Button and Check Boxes
We are at a crutial Point what we will do now is, so concentrate more on Panel2 and Panel3
1) Resize the First Pane we created
2) Add One more pane to Hold CheckBoxes
3) Add One more pane to Hold Radiobutton belonging to same Button Group
4) getPreferredSize() is responsible for the Size of Individual Panel height and width
5) ButtonGroup() is responsible for all 3 buttons belonging to same family, so only one gets selected at a time

//Requirements and Imports HelloWorldSwing.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HelloWorldSwing extends JFrame
{
 //Main starts the Application and Initialises the Constructor
 public static void main(String args[])
 {
   HelloWorldSwing app = new HelloWorldSwing();
 }
 //Constructor Defines the Frame Title and calls Setup Method
 public HelloWorldSwing() 
 {
  super("My Simple Application"); 
  setup();
  pack();  		 
  show();
}
//Setup Method is responsible for Creating Panel, Defining Size, adding Componets to the Panel
public void setup()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
	   
   //Panel 1 Start ------------------------------------------- 
   JPanel jpanel1 = new JPanel();
   jpanel1 = new JPanel()
   {
      public Dimension getPreferredSize()
      {
         Dimension size = super.getPreferredSize();
         size.width = 250;	
         size.height = 150;	
         return size;
      }
   };	
   jpanel1.setBorder
   (BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Border 1"));
   JLabel label1 = new JLabel("My Label");
   jpanel1.add(label1);	
   JTextField textbox1 = new JTextField("My TextBox",15);
   jpanel1.add(textbox1);	
   JLabel label2 = new JLabel(new ImageIcon("think.gif"));
   jpanel1.add(label2);	
   contentPane.add(jpanel1);
   
  //Panel 2 Start -------------------------------------------
  JPanel jpanel2 = new JPanel();
  jpanel2 = new JPanel()
  {
   public Dimension getPreferredSize()
   {
     Dimension size = super.getPreferredSize();
     size.width = 150;
     size.height = 150;	
     return size;
   }
  };
  jpanel2.setBorder
   (BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Border 2"));
  JCheckBox checkbox1 = new JCheckBox("Check Box1");
    jpanel2.add(checkbox1);	
  JCheckBox checkbox2 = new JCheckBox("Check Box2");	
    checkbox2.setSelected(false);
  jpanel2.add(checkbox2);	
    JCheckBox checkbox3 = new JCheckBox("Check Box3");	
  checkbox3.setSelected(true);
    jpanel2.add(checkbox3);
  JButton button1 = new JButton("My Button");
    jpanel1.add(button1);	
  contentPane.add(jpanel2);
	
 //Panel 3 Start -------------------------------------------
 ButtonGroup bg = new ButtonGroup();
 JPanel jpanel3 = new JPanel();
 jpanel3 = new JPanel()
 {
   public Dimension getPreferredSize()
   {
     Dimension size = super.getPreferredSize();
     size.width = 150;	
     size.height = 150;	
     return size;
   }
  };
jpanel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Border 3"));
  JRadioButton radio1 = new JRadioButton("Radio Button1",true);
    jpanel3.add(radio1);	
    bg.add(radio1);
  JRadioButton radio2 = new JRadioButton("Radio Button2",false);
    jpanel3.add(radio2);	
    bg.add(radio2);
  JRadioButton radio3 = new JRadioButton("Radio Button3",false);
    jpanel3.add(radio3);		
    bg.add(radio3);
  contentPane.add(jpanel3);
		
	   addWindowListener(new WindowEventHandler());	
	}
	//WindowEventHandler is responside for Closing the Application when close is clicked
	class WindowEventHandler extends WindowAdapter 
	{
	    public void windowClosing(WindowEvent e)
	    {
		   System.exit(0);
	    }
	}	
}
Recompile and Run the code again the output will be as
Mastering Gridbag Layout - Swing is All About Layouts and Placements
Once u are Familiar with the above layout placement , u can read more about working with layouts on Suns Official Website , now we move on the the most complex layout that is GridBag Layout
First switch off your computer monitor (Not if u are reading this :-) ) take a paper and draw your screen in rows and cols forming a Matrix

Now Observer the Matrix first box is 0,0 , 0th rows and 0th cols
Row 0 => Label Movie Database in 2nd Col , width = 2cols
Row 1 => Title Col 0 :: Actual Number Col 1 :: SL No Col 2 :: Actual No Col 3 ::Col 4 Blank :: Col5 first Button :: Col6 Prev :: Col7 Next :: Col8 Last
Row 2 => Label Star Name in 0th Col :: Actual Name in Col 1
Row 3 => Label Raings in 0th Col :: Ratings Radio Buttons in Col 1 :: Image Corresponding to CD


Since This is Fairly Large Code what we will do now is Develop First 3 Rows First later go for the 4th Row Ofcourse there will be slight deviation over the plan on paper and placing of components as a programmer u would agree with that, Swing is not as Easy as Drawing on a paper you would tell this your Superior
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GBLayout extends JFrame
{
 public static void main(String args[])
 {
   GBLayout app = new GBLayout();
 }
 public GBLayout() 
 {
   super("James Smith's Java"); 
   setup();
   pack();  		 
   show();
 }
 public void setup()
 {
   Font dataFont = new Font("courier new",Font.PLAIN,12);
   Font titleFont = new Font("courier new",Font.BOLD,14);
   Font titleFont1 = new Font("courier new",Font.BOLD,12);

   GridBagLayout gridbag = new GridBagLayout();
   GridBagConstraints c = new GridBagConstraints();
   c.fill = GridBagConstraints.HORIZONTAL;

   Container contentPane = getContentPane();		
   contentPane.setLayout(gridbag);

   JPanel jpanel1 = new JPanel();		
   jpanel1 = new JPanel() 
   {
     public Dimension getPreferredSize() 
     {
  	   Dimension size = super.getPreferredSize();
  	   size.width = 700;
       size.height = 100;	
  	   return size;
     }
   };	
  jpanel1.setBorder
  (BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
  "Video CD Database Application"));
  jpanel1.setLayout(gridbag);	

  // Row 1 Components -------------------------------------------	    
  JLabel jlabel1 = new JLabel(" Movie Database  ");
  jlabel1.setFont(titleFont);
  c.fill = GridBagConstraints.BOTH;	
  c.weightx = 0.5; //Makes Component Spread Wider			
  c.gridwidth = 1; //1 columns wide				
  c.gridx = 2;     //3rd Col  -- Very Important 					   
  c.gridy = 0;     //0th Row  -- Very Important 	
  gridbag.setConstraints(jlabel1, c);				
  jpanel1.add(jlabel1);
  
  // Row 2 Components -------------------------------------------	    
  JLabel jlabel2 = new JLabel(" Title : ");
  jlabel2.setFont(titleFont1);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 0;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(jlabel2, c);				
  jpanel1.add(jlabel2);

  JLabel jlabel3 = new JLabel(" Marrying Rich Men ");
  jlabel3.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 1;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(jlabel3, c);				
  jpanel1.add(jlabel3);	

  JLabel jlabel4 = new JLabel(" Sl No : ");
  jlabel4.setFont(titleFont1);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 2;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(jlabel4, c);				
  jpanel1.add(jlabel4);	

  JLabel jlabel5 = new JLabel(" 12 ");
  jlabel5.setFont(dataFont);		
  c.fill = GridBagConstraints.WEST;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 3;       						     
  c.gridy = 1;     
  c.insets = new Insets(0,0,0,20);  //(top.left,bottom,right) Padding	
  gridbag.setConstraints(jlabel5, c);				
  jpanel1.add(jlabel5);

  JButton buttonF = new JButton(new ImageIcon("first.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 4;       						     
  c.gridy = 1;    
  c.insets = new Insets(0,0,0,0);       						
  gridbag.setConstraints(buttonF, c);				
  jpanel1.add(buttonF);	

  JButton buttonP = new JButton(new ImageIcon("back.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 5;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(buttonP, c);				
  jpanel1.add(buttonP);

  JButton buttonN = new JButton(new ImageIcon("next.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 6;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(buttonN, c);				
  jpanel1.add(buttonN);

  JButton buttonL = new JButton(new ImageIcon("last.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 7;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(buttonL, c);				
  jpanel1.add(buttonL);		

  //Row 3 Components -----------------------------------------
  JLabel jlabel6 = new JLabel(" Star : ");
  jlabel6.setFont(titleFont1);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 0;       						     
  c.gridy = 2;       						
  gridbag.setConstraints(jlabel6, c);				
  jpanel1.add(jlabel6);	

  JLabel jlabel7 = new JLabel(" Anna Nicole Smith ");
  jlabel7.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 1;       						     
  c.gridy = 2;       						
  gridbag.setConstraints(jlabel7, c);				
  jpanel1.add(jlabel7);	

  addWindowListener(new WindowEventHandler());	
  contentPane.add(jpanel1);
}

 class WindowEventHandler extends WindowAdapter 
 {
   public void windowClosing(WindowEvent e)
   {
     System.exit(0);
   }
 }
}

Click on The Image to Enlarge
Since we have gone too far without Theory ! Have Coffee and Go through some Theory
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; this tends to result in more predictable layouts.

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). Note: GridBagLayout doesn't allow components to span multiple rows unless the component is in the leftmost column or you've specified positive gridx and gridy values for the component.

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) are NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but don't change its height), VERTICAL (make the component tall enough to fill its display area vertically, but don't change its width), and BOTH (make the component fill its display area entirely).

ipadx, ipady
Specifies the internal padding: how much to add to the minimum 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.

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.

anchor
Used when the component is smaller than its display area to determine where (within the area) to place the component. Valid values (defined as GridBagConstraints constants) are CENTER (the default), PAGE_START, PAGE_END, LINE_START, LINE_END, FIRST_LINE_START, FIRST_LINE_END, LAST_LINE_END, and LAST_LINE_START.

Here is a picture of how these values are interpreted in a container that has the default, left-to-right component orientation.

-----------------------------------------------------
|FIRST_LINE_START   PAGE_START     FIRST_LINE_END|
|                                                |
|                                                |
|LINE_START           CENTER             LINE_END|
|                                                |
|                                                |
|LAST_LINE_START     PAGE_END       LAST_LINE_END|
-----------------------------------------------------
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 nonzero 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.

Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the right most column and bottom row.

Coming Back to Our Code , we will build the Third Row with the Image Display
Now concentrate on Adding Radio Buttons in Blue and Observe the useage of ButtonGroup()
Increase the Preferred Size in Red
Observe how images are inside labels using ImageIcon() in Green
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GBLayout extends JFrame
{
  public static void main(String args[])
  {
    GBLayout app = new GBLayout();
  }
  public GBLayout() 
  {
    super("James Smith's Java"); 
    setup();
    pack();  					 
    show();
 }
 public void setup()
 {
   Font dataFont = new Font("courier new",Font.PLAIN,12);
   Font titleFont = new Font("courier new",Font.BOLD,14);
   Font titleFont1 = new Font("courier new",Font.BOLD,12);

   GridBagLayout gridbag = new GridBagLayout();
   GridBagConstraints c = new GridBagConstraints();		       
   c.fill = GridBagConstraints.HORIZONTAL;

   Container contentPane = getContentPane();		
   contentPane.setLayout(gridbag);

   JPanel jpanel1 = new JPanel();		
   jpanel1 = new JPanel() 
   {
     public Dimension getPreferredSize() 
     {
     Dimension size = super.getPreferredSize();
     size.width = 700;	
     size.height = 400;	
     return size;
     }
   };	
   jpanel1.
   setBorder(BorderFactory.
   createTitledBorder(BorderFactory.createEtchedBorder(),"Video CD Database Application"));
   jpanel1.setLayout(gridbag);	

   // Row 1 Components -------------------------------------------	    
   JLabel jlabel4 = new JLabel(" Sl No : ");
   jlabel4.setFont(titleFont1);		
   c.fill = GridBagConstraints.BOTH;		
   c.ipady = 1;       						
   c.ipadx = 1;  
   c.gridx = 0;       						     
   c.gridy = 0;       						
   gridbag.setConstraints(jlabel4, c);				
   jpanel1.add(jlabel4);	

   JLabel jlabel5 = new JLabel(" 12 ");
   jlabel5.setFont(dataFont);		
   c.fill = GridBagConstraints.BOTH;		
   c.ipady = 1;       						
   c.ipadx = 1;  
   c.gridx = 1;       						     
   c.gridy = 0;     
   gridbag.setConstraints(jlabel5, c);				
   jpanel1.add(jlabel5);

  JLabel jlabel1 = new JLabel(" Movie Database  ");
  jlabel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
  jlabel1.setFont(titleFont);
  c.fill = GridBagConstraints.BOTH;	
  c.weightx = 0.5;				
  c.gridx = 2;       	//3rd Col  -- Very Important
  c.gridy = 0;       	//0th Row  -- Very Important
  gridbag.setConstraints(jlabel1, c);				
  jpanel1.add(jlabel1);
  // Row 2 Components -------------------------------------------	    
  JLabel jlabel2 = new JLabel(" Title : ");
  jlabel2.setFont(titleFont1);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 0;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(jlabel2, c);				
  jpanel1.add(jlabel2);	

  JLabel jlabel3 = new JLabel(" Blonde Babes ");
  jlabel3.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 1;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(jlabel3, c);				
  jpanel1.add(jlabel3);	

  JButton buttonF = new JButton(new ImageIcon("first.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 4;       						     
  c.gridy = 1;    
  c.insets = new Insets(0,0,0,0);       						
  gridbag.setConstraints(buttonF, c);				
  jpanel1.add(buttonF);	

  JButton buttonP = new JButton(new ImageIcon("back.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 5;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(buttonP, c);				
  jpanel1.add(buttonP);

  JButton buttonN = new JButton(new ImageIcon("next.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 6;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(buttonN, c);				
  jpanel1.add(buttonN);

  JButton buttonL = new JButton(new ImageIcon("last.gif"));
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 7;       						     
  c.gridy = 1;       						
  gridbag.setConstraints(buttonL, c);				
  jpanel1.add(buttonL);		

  //Row 3 Components -----------------------------------------
  JLabel jlabel6 = new JLabel(" Star : ");
  jlabel6.setFont(titleFont1);		
  c.fill = GridBagConstraints.BOTH;	
  c.insets = new Insets(5,0,0,0); 	
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 0;       						     
  c.gridy = 2;       						
  gridbag.setConstraints(jlabel6, c);				
  jpanel1.add(jlabel6);	

  JLabel jlabel7 = new JLabel(" Anna Nicole ");
  jlabel7.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;				 
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 1;       						     
  c.gridy = 2;       						
  gridbag.setConstraints(jlabel7, c);				
  jpanel1.add(jlabel7);	

  //Row 4 ---------------------------------------------------  	
  JLabel jlabel8 = new JLabel(" Ratings : ");
  jlabel8.setFont(titleFont1);		
  c.fill = GridBagConstraints.BOTH;		
  c.insets = new Insets(5,0,0,0);   
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 0;       						     
  c.gridy = 3;       						
  gridbag.setConstraints(jlabel8, c);				
  jpanel1.add(jlabel8);	

  ButtonGroup bg = new ButtonGroup();
  JRadioButton radio1 = new JRadioButton("Adult Video",false);
  radio1.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 1;       						     
  c.gridy = 3;       						
  bg.add(radio1);
  gridbag.setConstraints(radio1, c);				
  jpanel1.add(radio1);

  JRadioButton radio2 = new JRadioButton("General Viewing",true);
  radio2.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 2;       						     
  c.gridy = 3;       					
  bg.add(radio2);	
  gridbag.setConstraints(radio2, c);				
  jpanel1.add(radio2);

  JRadioButton radio3 = new JRadioButton("Childrens",false);
  radio3.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;		
  c.ipady = 1;       						
  c.ipadx = 1;  
  c.gridx = 3;       						     
  c.gridy = 3; 
  bg.add(radio3);      						
  gridbag.setConstraints(radio3, c);				
  jpanel1.add(radio3);

  // Row 5 -------------------------------------------
  JLabel labelim = new JLabel(new ImageIcon("Anna.jpg"));
  labelim.setFont(dataFont);		
  c.fill = GridBagConstraints.BOTH;				
  c.ipady = 1;       						
  c.ipadx = 1;  
  labelim.setPreferredSize(new Dimension(200, 221));
  c.gridx = 2;       						     
  c.gridy = 4;       						
  gridbag.setConstraints(labelim, c);				
  jpanel1.add(labelim);

  addWindowListener(new WindowEventHandler());	
  contentPane.add(jpanel1);
}
 class WindowEventHandler extends WindowAdapter 
 {
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
 }	
}
Click on The Image to Enlarge


What Next ? Mastering Java Swing Part 2 : Building Menus and Event Handling Making Applications Look WOW .....

 My Dream to be your Friend and Create a Group of Intelligent and Understanding Programmers
     If you like this article and/or code mailme or Join our small Java User Group which is by the Programmers for the Programmers ,
Till we meet next time BYE      Kind Regards - James Smith

  Java, J2EE, J2SE and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc.
in the United States and other countries.