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

 

TableLayoutConstraints

The preceeding examples have expressed the layout constraints in terms of a simple string. This is a convenient short-hand notation, but it is not the only way to express constraints. The TableLayoutConstraints class provides a means of manipulating constraints as objects. The following three blocks of code are equivalent.

container.add (firstComponent, "1, 2, R, B");
container.add (secondComponent, "5, 8, 6, 10");
                      
 
container.add (firstComponent,
    new TableLayoutConstraints(1, 2, 1, 2, TableLayout.RIGHT, TableLayout.BOTTOM));

container.add (secondComponent,
    new TableLayoutConstraints(5, 8, 6, 10, TableLayout.FULL, TableLayout.FULL));
                      
 
TableLayoutConstraints c = new TableLayoutConstraints();

c.col1 = c.col2 = 1;
c.row1 = c.row2 = 2;
c.hAlign = TableLayout.RIGHT;
c.vAlign = TableLayout.BOTTOM;

container.add (firstComponent, c);

c.col1 = 5;
c.col2 = 6;
c.row1 = 8;
c.row2 = 10;

container.add (secondComponent, c);
                      

There are two constructors for TableLayoutConstraints. One takes no parameters and defaults to the short-hand "0, 0" or occupy a single cell at (0, 0) with full horizontal and vertical justification. The second constructor takes six parameters: col1, row1, col2, row2, hAlign, and vAlign. The first pair is the upper-left corner of the cell set. The second pair is the lower-right corner. If the first and second pair match, the constraint refers to a single cell and the third pair is used for justification.

Since the fields of TableLayoutConstraints are public, they may be manipulated directly. This was done for convenience. The third example shows the fields being manipulated to set constraints. Since TableLayout will make a copy of the TableLayoutConstraints, you can use the same constraints object when setting the constraints for more than one component.

TableLayout has two methods pertaining to TableLayoutConstraints.

getContstraints(Component)
returns the TableLayoutConstraints associated with the given component.
setConstraints(Component, TableLayoutConstraints)
replaces the given component's constraints with the newly supplied constraints. The next example will make use of these methods.

Previous | Next