HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Becoming Friends with GridBagLayout"
Vol. 3, Issue 4, p. 24

	

Listing 1.
  
import java.awt.*;  

public class gbl1  
{  

 List  sourceList;  
 List  destinationList;  
 Button  addButton;  
 Button  addAllButton;  
 Button  removeButton;  
 Button  removeAllButton;  
 Button  OKButton;  
 Button  cancelButton;  
 Label  sourceLabel;  
 Label  destinationLabel;  
 Frame  aFrame;  

 GridBagLayout  gbl;  
 GridBagConstraints gbc;  
   

 public gbl1()  
 {  
  sourceList  = new List();  
  destinationList  = new List();  
  addButton  = new Button( " >" );  
  addAllButton  = new Button( ">>" );  
  removeButton  = new Button( " >" );  
  removeAllButton = new Button( ">>" );  
  OKButton  = new Button( "Ok" );  
  cancelButton  = new Button( "Cancel" );  
  sourceLabel  = new Label( "Source" );  
  destinationLabel = new Label( "Destination" );  
  aFrame   = new Frame();  
  gbl   = new GridBagLayout();  
  gbc   = new GridBagConstraints();  

  aFrame.setLayout( gbl );  
  buildFrame();  
  aFrame.setSize( 300,200 );  
  aFrame.show();  
 }  
   

 public void buildFrame()  
 {  
  addComponent( 0,0,1,1,aFrame,sourceLabel );  
  addComponent( 2,0,1,1,aFrame,destinationLabel );  
  addComponent( 0,1,1,4,aFrame,sourceList );  
  addComponent( 2,1,1,4,aFrame,destinationList );  
  addComponent( 1,1,1,1,aFrame,addButton );  
  addComponent( 1,2,1,1,aFrame,addAllButton );  
  addComponent( 1,3,1,1,aFrame,removeButton );  
  addComponent( 1,4,1,1,aFrame,removeAllButton );  
  addComponent( 0,5,1,1,aFrame,OKButton );    
  addComponent( 2,5,1,1,aFrame,cancelButton );  
 }  
   
 /**  
 * A helper method to add Components to a Container using   
* GridBagLayout  
*/  
public void addComponent( int x, int y, int w, int h, Container aContainer, Component aComponent )  
 {  
  gbc.gridx = x;  
  gbc.gridy = y;  
  gbc.gridwidth = w;  
  gbc.gridheight = h;  
  gbl.setConstraints( aComponent, gbc );  
  aContainer.add( aComponent );  
 }  
  
   

 public static void main( String[] args )  
 {  
  gbl1 myLayout = new gbl1();  
 }  
   

}// end class  

Listing 2.
  
 public void buildFrame()  
 {  
  gbc.anchor = gbc.EAST;  
  addComponent( 0,0,1,1,aFrame,sourceLabel );  
  gbc.anchor = gbc.WEST;  
  addComponent( 2,0,1,1,aFrame,destinationLabel );  
  gbc.anchor = gbc.CENTER;  // back to default  
  addComponent( 0,1,1,4,aFrame,sourceList );  
  addComponent( 2,1,1,4,aFrame,destinationList );  
  addComponent( 1,1,1,1,aFrame,addButton );  
  addComponent( 1,2,1,1,aFrame,addAllButton );  
  addComponent( 1,3,1,1,aFrame,removeButton );  
  addComponent( 1,4,1,1,aFrame,removeAllButton );  
  gbc.anchor = gbc.EAST;  
  addComponent( 0,5,1,1,aFrame,OKButton );   
  gbc.anchor = gbc.WEST;    
  addComponent( 2,5,1,1,aFrame,cancelButton );  
 }  

Listing 3
  
import java.awt.*;  

public class gbl1  
{  

 List  sourceList;  
 List  destinationList;  
 Button  addButton;  
 Button  addAllButton;  
 Button  removeButton;  
 Button  removeAllButton;  
 Button  OKButton;  
 Button  cancelButton;  
 Label  sourceLabel;  
 Label  destinationLabel;  
 Frame  aFrame;  

 GridBagLayout  gbl;  
 GridBagConstraints gbc;  
   

 public gbl1()  
 {  
  sourceList  = new List();  
  destinationList  = new List();  
  addButton  = new Button( " >" );  
  addAllButton  = new Button( ">>" );  
  removeButton  = new Button( " >" );  
  removeAllButton = new Button( ">>" );  
  OKButton  = new Button( "Ok" );  
  cancelButton  = new Button( "Cancel" );  
  sourceLabel  = new Label( "Source" );  
  destinationLabel = new Label( "Destination" );  

  sourceLabel.setAlignment ( Label.CENTER );  
  destinationLabel.setAlignment ( Label.CENTER );  
  aFrame    = new Frame();  
  gbl    = new GridBagLayout();  
  gbc    = new GridBagConstraints();  

  aFrame.setLayout( gbl );  
  buildFrame();  
  aFrame.setSize( 300,200 );  
  aFrame.show();  
 }  

 public void buildFrame()  
 {    
 addComponent(0,0,1,1,gbc.HORIZONTAL,1,1,aFrame,sourceLabel);  
 addComponent(2,0,1,1,gbc.HORIZONTAL,1,1,aFrame,destinationLabel );  
  addComponent( 0,1,1,4,gbc.BOTH,10,10,aFrame,sourceList );  
  addComponent( 2,1,1,4,gbc.BOTH,10,10,aFrame,destinationList );  
  addComponent( 1,1,1,1,gbc.NONE,1,1,aFrame,addButton );  
  addComponent( 1,2,1,1,gbc.NONE,1,1,aFrame,addAllButton );  
  addComponent( 1,3,1,1,gbc.NONE,1,1,aFrame,removeButton );  
  addComponent( 1,4,1,1,gbc.NONE,1,1,aFrame,removeAllButton );    
  addComponent( 0,5,1,1,gbc.HORIZONTAL,0,2,aFrame,OKButton );   
  addComponent( 2,5,1,1,gbc.HORIZONTAL,0,2,aFrame,cancelButton );  
 }  

public void addComponent( int x, int y, int w, int h, int fill, int xWeight, int yWeight, Container aContainer, Component aComponent )  
 {  
  gbc.gridx = x;  
  gbc.gridy = y;  
  gbc.gridwidth = w;  
  gbc.gridheight = h;  
  gbc.fill = fill;  
  gbc.weightx = xWeight;  
  gbc.weighty = yWeight;  
  gbl.setConstraints( aComponent, gbc );  
  aContainer.add( aComponent );  
 }  
  
   

 public static void main( String[] args )  
 {  
  gbl1 myLayout = new gbl1();  
 }  
   

}// end class
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.