TableLayout TableLayout

Java Technology Home Page
Downloads, APIs, Documentation
Java Developer Connection
Docs, Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide


TSC Contents
Front Page
Article Index

Related Links
JFC Home
Download J2SE
J2SE 1.4 Release
Notes for Swing
Java Tutorial
Java Web Start
Swing Sightings

Sign up for email
notification

 

 Article Table of Contents 
    
 Introduction 
  What is TableLayout? 
  Creating a TableLayout 
    
 Column and Row Sizes 
  Absolute and Scalable Space 
  Order of Allocation 
  Rounding Considerations 
    
 Cells 
  Adding Components 
  Justification 
  Multiple Cells 
    
 Final Theory 
  Dynamic Rows and Columns 
  Preferred Layout Size 
  Onward 
    
 Examples 
  Simple 
 GridLayout Comparison 
  GridBagLayout Comparison 
  A Typical GUI 
  Preferred Rows and Columns 
  TableLayoutConstraints 
  A RAD Tool 
    
 API Reference 
 Download 
Help pagesA-Z Index

The Source for Java Technology

 

GridLayout Versus TableLayout

This example shows how TableLayout offers more functionality than GridLayout. The showGridWindow method utilizes GridLayout to lay out its components. The showTableWindow method utilizes TableLayout to provide the same layout. The showTableWindow2 method utilizes TableLayout but allows cells to be selectively empty -- a feature that GridLayout cannot support. The output of this example is shown below.

package example;



import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import layout.TableLayout;



public class GridVersusTable
{

    
    
    protected static Frame showGridWindow ()
    {
        // Create frame
        Frame frame = new Frame("GridLayout");
        frame.setFont (new Font("Helvetica", Font.PLAIN, 14));
        frame.setLayout (new GridLayout(2, 0));
   
        // Create and add buttons
        frame.add (new JButton("One"));
        frame.add (new JButton("Two"));
        frame.add (new JButton("Three"));
        frame.add (new JButton("Four"));

        // Show frame
        frame.pack();
        frame.setLocation (0, 10);
        frame.show();

        return frame;
    }



    protected static Frame showTableWindow ()
    {
        // Create frame
        Frame frame = new Frame("TableLayout");
        frame.setFont (new Font("Helvetica", Font.PLAIN, 14));
        
        // Set layout
        double f = TableLayout.FILL;
        double size[][] = {{f, f}, {f, f}};
        frame.setLayout (new TableLayout(size));
        
        // Create and add buttons
        frame.add (new JButton("One"),   "0, 0");
        frame.add (new JButton("Two"),   "1, 0");
        frame.add (new JButton("Three"), "0, 1");
        frame.add (new JButton("Four"),  "1, 1");

        // Show frame
        frame.pack();
        frame.setLocation (200, 10);
        frame.show();

        return frame;
    }



    protected static Frame showTableWindow2 ()
    {
        // Create frame
        Frame frame = new Frame("TableLayout");
        frame.setFont (new Font("Helvetica", Font.PLAIN, 14));
        
        // Set layout
        double f = TableLayout.FILL;
        double size[][] = {{f, f}, {f, f}};
        frame.setLayout (new TableLayout(size));
        
        // Create and add buttons
        frame.add (new JButton("One"),   "0, 0");
        frame.add (new JButton("Two"),   "1, 1");

        // Show frame
        frame.pack();
        frame.setLocation (400, 10);
        frame.show();

        return frame;
    }



    public static void main (String args[])
    {
        WindowListener listener =
            (new WindowAdapter()
                {
                    public void windowClosing (WindowEvent e)
                    {
                        System.exit (0);
                    }
                }
            );

        Frame frame = showGridWindow();
        frame.addWindowListener(listener);
        
        frame = showTableWindow();
        frame.addWindowListener(listener);

        frame = showTableWindow2();
        frame.addWindowListener(listener);
    }
}
                

This illustration shows the three frames created by the above program. The first two frames function identically, showing that TableLayout subsumes the functionality of GridLayout. TableLayout does this without requiring more complex code. The third frame in the illustration shows where TableLayout surpasses GridLayout. TableLayout allows cells to be skipped. It also allows the rows and columns to have different sizes. GridLayout does not. GridBagLayout also subsumes the functionality of GridLayout, but at the cost of complexity as our next example will show.

Previous | Next