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

 

Preferred Rows and Columns

An example of TableLayout using preferred rows and columns.

package example;



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



public class Preferred extends JFrame
{
    
    
    
    public static void main (String args[])
    {
        new Preferred();
    }
    
    
    
    public Preferred ()
    {
        super ("The Power of Preferred Sizes");
        
        Container pane = getContentPane();
        
        // b - border
        // f - FILL
        // p - PREFERRED
        // vs - vertical space between labels and text fields
        // vg - vertical gap between form elements
        // hg - horizontal gap between form elements
        
        double b = 10;
        double f = TableLayout.FILL;
        double p = TableLayout.PREFERRED;
        double vs = 5;
        double vg = 10;
        double hg = 10;
        
        double size[][] =
            {{b, f, hg, p, hg, p, b},
             {b, p, vs, p, vg, p, vs, p, vg, p, vs, p, vg, p, b}};
             
        TableLayout layout = new TableLayout(size);
        pane.setLayout (layout);
        
        // Create all controls
        JLabel labelName    = new JLabel("Name");
        JLabel labelAddress = new JLabel("Address");
        JLabel labelCity    = new JLabel("City");
        JLabel labelState   = new JLabel("State");
        JLabel labelZip     = new JLabel("Zip");
        
        JTextField textfieldName    = new JTextField(10);
        JTextField textfieldAddress = new JTextField(20);
        JTextField textfieldCity    = new JTextField(10);
        JTextField textfieldState   = new JTextField(2);
        JTextField textfieldZip     = new JTextField(5);
        
        JButton buttonOk = new JButton("OK");
        JButton buttonCancel = new JButton("Cancel");
        JPanel panelButton = new JPanel();
        panelButton.add (buttonOk);
        panelButton.add (buttonCancel);
        
        // Add all controls
        pane.add (labelName,        "1,  1, 5, 1");
        pane.add (textfieldName,    "1,  3, 5, 3");
        pane.add (labelAddress,     "1,  5, 5, 5");
        pane.add (textfieldAddress, "1,  7, 5, 7");
        pane.add (labelCity,        "1,  9");
        pane.add (textfieldCity,    "1, 11");
        pane.add (labelState,       "3,  9");
        pane.add (textfieldState,   "3, 11");
        pane.add (labelZip,         "5,  9");
        pane.add (textfieldZip,     "5, 11");
        pane.add (panelButton,      "1, 13, 5, 13");
        
        allowClosing();
        pack();
        setResizable (false);
        show();
    }
    
    
    
    public void allowClosing ()
    {
        addWindowListener
            (new WindowAdapter()
                {
                    public void windowClosing (WindowEvent e)
                    {
                        System.exit (0);
                    }
                }
            );
    }
    
    
    
}
                

This example uses only absolute and preferred columns. If fact the frame itself is not resizable. The purpose of using TableLayout is several fold. First, no sizes or absolute positions of controls need to be hard-coded. This makes the code more flexible and allows different fonts and control to be used. Second, without a RAD tool, it is easier to construct the GUI using TableLayout than by putting in absolute positions by hand. Third, it is easier to see what is being done in the code than with hard-coded component bounds. Fourth, the spacing and borders can be adjusted easily. Another reason is a philosophical issue. Using a layout manager is a more elegant way of putting together a GUI. The final benefit to using TableLayout even for non-resizble containers is that the preferred size of the container is figured out for you.

A simple technique to quickly design a GUI is to use a humble pen and paper. Sketch what you want the GUI to look like, and then use a straight edge to form rows and columns. Make sure to include the gaps between components so that you can easily adjust them. Then number the rows and columns and assign sizes to them based on whether or not you want their components to be resized in the horizontal or vertical direction. Once this is done, the code is easy to construct and will look just like your sketch.

Previous | Next