wyvern.util
Class TableLayout

java.lang.Object
  extended bywyvern.util.TableLayout
All Implemented Interfaces:
java.awt.LayoutManager, java.awt.LayoutManager2, java.io.Serializable, TableLayoutConstants

public class TableLayout
extends java.lang.Object
implements java.awt.LayoutManager2, java.io.Serializable, TableLayoutConstants

TableLayout is a layout manager that arranges components in rows and columns like a spreadsheet. TableLayout allows each row or column to be a different size. A row or column can be given an absolute size in pixels, a percentage of the available space, or it can grow and shrink to fill the remaining space after other rows and columns have been resized.

Using spreadsheet terminology, a cell is the intersection of a row and column. Cells have finite, non-negative sizes measured in pixels. The dimensions of a cell depend solely upon the dimensions of its row and column.

A component occupies a rectangular group of one or more cells. The component can be aligned in four ways within that cell.

A component can be stretched horizontally to fit the cell set (full justification), or it can be placed in the center of the cell. The component could also be left justified or right justified. Similarly, the component can be full, center, top, or bottom justified along the

 public static void main (String args[])
 {
     // Create a frame
     Frame frame = new Frame("Example of TableLayout");
     frame.setBounds (100, 100, 300, 300);
 
     // Create a TableLayout for the frame
     double border = 10;
     double size[][] =
         {{border, 0.10, 20, TableLayout.FILL, 20, 0.20, border},  // Columns
          {border, 0.20, 20, TableLayout.FILL, 20, 0.20, border}}; // Rows
 
     frame.setLayout (new TableLayout(size));
 
     // Create some buttons
     String label[] = {"Top", "Bottom", "Left", "Right", "Center", "Overlap"};
     Button button[] = new Button[label.length];
 
     for (int i = 0; i < label.length; i++)
         button[i] = new Button(label[i]);
 
     // Add buttons
     frame.add (button[0], "1, 1, 5, 1"); // Top
     frame.add (button[1], "1, 5, 5, 5"); // Bottom
     frame.add (button[2], "1, 3      "); // Left
     frame.add (button[3], "5, 3      "); // Right
     frame.add (button[4], "3, 3, c, c"); // Center
     frame.add (button[5], "3, 3, 3, 5"); // Overlap
 
     // Allow user to close the window to terminate the program
     frame.addWindowListener
         (new WindowListener()
             {
                 public void windowClosing (WindowEvent e)
                 {
                     System.exit (0);
                 }
 
                 public void windowOpened (WindowEvent e) {}
                 public void windowClosed (WindowEvent e) {}
                 public void windowIconified (WindowEvent e) {}
                 public void windowDeiconified (WindowEvent e) {}
                 public void windowActivated (WindowEvent e) {}
                 public void windowDeactivated (WindowEvent e) {}
             }
         );
 
     // Show frame
     frame.show();
 }
 

Version:
2.1 4/26/02
Author:
Daniel E. Barbalace
See Also:
Serialized Form

Nested Class Summary
protected static class TableLayout.Entry
           
 
Field Summary
protected static int C
          Indicates a column
protected  int[][] crOffset
          Offsets of crs in pixels.
protected  int[][] crSize
          Sizes of crs in pixels
protected  double[][] crSpec
          Sizes of crs expressed in absolute and relative terms
protected static double[][] defaultSize
          Default row/column size
protected  boolean dirty
          Indicates whether or not the size of the cells are known for the last known size of the container.
protected  int hGap
          Horizontal gap between columns
protected  java.util.LinkedList list
          List of components and their sizes
protected  int oldHeight
          Previous known height of the container
protected  int oldWidth
          Previous known width of the container
protected static int R
          Indicates a row
protected  int vGap
          Vertical gap between rows
 
Fields inherited from interface wyvern.util.TableLayoutConstants
BOTTOM, CENTER, FILL, FULL, LEFT, MAX_ALIGN, MIN_ALIGN, MINIMUM, PREFERRED, RIGHT, TOP
 
Constructor Summary
TableLayout()
          Constructs an instance of TableLayout.
TableLayout(double[][] size)
          Constructs an instance of TableLayout.
 
Method Summary
 void addLayoutComponent(java.awt.Component component, java.lang.Object constraint)
          Adds the specified component with the specified name to the layout.
 void addLayoutComponent(java.lang.String name, java.awt.Component component)
          Adds the specified component with the specified name to the layout.
protected  int assignAbsoluteSize(int z, int availableSize)
          Assigns absolute sizes.
protected  void assignFillSize(int z, int availableSize)
          Assigns FILL sizes.
protected  int assignPrefMinSize(int z, int availableSize, double typeOfSize)
          Assigned widths to preferred and minimum size columns and rows.
protected  int assignRelativeSize(int z, int availableSize)
          Assigns relative sizes.
protected  java.awt.Dimension calculateLayoutSize(java.awt.Container container, double typeOfSize)
          Calculates the preferred or minimum size for the methods preferredLayoutSize and minimumLayoutSize.
protected  int calculateLayoutSize(java.awt.Container container, int z, double typeOfSize, TableLayout.Entry[] entryList, java.awt.Dimension[] prefMinSize)
          Calculates the preferred or minimum size for the method calculateLayoutSize(Container container, double typeOfSize).
protected  void calculateOffset(int z, java.awt.Insets inset)
          Calculates the offset of each cr.
protected  void calculateSize(java.awt.Container container)
          Calculates the sizes of the rows and columns based on the absolute and relative sizes specified in crSpec[R] and crSpec[C] and the size of the container.
protected  int[] calculateSizeAndOffset(TableLayout.Entry entry, int preferredSize, boolean isColumn)
          Calculates the vertical/horizontal offset and size of a component.
 void deleteColumn(int i)
          Deletes a column in this layout.
protected  void deleteCr(int z, int i)
          Deletes a cr for the methods deleteRow or deleteColumn.
 void deleteRow(int i)
          Deletes a row in this layout.
 double[] getColumn()
          Gets the sizes of columns in this layout.
 double getColumn(int i)
          Gets the width of a single column in this layout.
 TableLayoutConstraints getConstraints(java.awt.Component component)
          Gets the constraints of a given component.
 int getHGap()
          Gets the horizontal gap between colunns.
 java.awt.Component[] getInvalidEntry()
          Determines whether or not there are any components with invalid constraints.
 float getLayoutAlignmentX(java.awt.Container parent)
          Returns the alignment along the x axis.
 float getLayoutAlignmentY(java.awt.Container parent)
          Returns the alignment along the y axis.
 int getNumColumn()
          Gets the number of columns in this layout.
 int getNumRow()
          Gets the number of rows in this layout.
 java.awt.Component[] getOverlappingEntry()
          Gets a list of overlapping components.
 double[] getRow()
          Gets the height of a single row in this layout.
 double getRow(int i)
          Gets the sizes of a row in this layout.
 int getVGap()
          Gets the vertical gap between rows.
 void insertColumn(int i, double size)
          Inserts a column in this layout.
 void insertCr(int z, int i, double size)
          Inserts a cr for the methods insertRow or insertColumn.
 void insertRow(int i, double size)
          Inserts a row in this layout.
 void invalidateLayout(java.awt.Container target)
          Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
 void layoutContainer(java.awt.Container container)
          To lay out the specified container using this layout.
static void main(java.lang.String[] args)
           
 java.awt.Dimension maximumLayoutSize(java.awt.Container target)
          Returns the maximum dimensions for this layout given the components in the specified target container.
 java.awt.Dimension minimumLayoutSize(java.awt.Container container)
          Determines the minimum size of the container argument using this layout.
 java.awt.Dimension preferredLayoutSize(java.awt.Container container)
          Determines the preferred size of the container argument using this layout.
 void removeLayoutComponent(java.awt.Component component)
          Removes the specified component from the layout.
 void setColumn(double[] column)
          Adjusts the number and sizes of rows in this layout.
 void setColumn(int i, double size)
          Adjusts the width of a single column in this layout.
 void setConstraints(java.awt.Component component, TableLayoutConstraints constraint)
          Sets the constraints of a given component.
protected  void setCr(int z, double[] size)
          Sets the sizes of rows or columns for the methods setRow or setColumn.
protected  void setCr(int z, int i, double size)
          Sets the sizes of rows or columns for the methods setRow or setColumn.
 void setHGap(int hGap)
          Sets the horizontal gap between colunns.
 void setRow(double[] row)
          Adjusts the number and sizes of rows in this layout.
 void setRow(int i, double size)
          Adjusts the height of a single row in this layout.
 void setVGap(int vGap)
          Sets the vertical gap between rows.
 java.lang.String toString()
          Converts this TableLayout to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultSize

protected static final double[][] defaultSize
Default row/column size


C

protected static final int C
Indicates a column

See Also:
Constant Field Values

R

protected static final int R
Indicates a row

See Also:
Constant Field Values

crSpec

protected double[][] crSpec
Sizes of crs expressed in absolute and relative terms


crSize

protected int[][] crSize
Sizes of crs in pixels


crOffset

protected int[][] crOffset
Offsets of crs in pixels. The left boarder of column n is at crOffset[C][n] and the right boarder is at cr[C][n + 1] for all columns including the last one. crOffset[C].length = crSize[C].length + 1


list

protected java.util.LinkedList list
List of components and their sizes


dirty

protected boolean dirty
Indicates whether or not the size of the cells are known for the last known size of the container. If dirty is true or the container has been resized, the cell sizes must be recalculated using calculateSize.


oldWidth

protected int oldWidth
Previous known width of the container


oldHeight

protected int oldHeight
Previous known height of the container


hGap

protected int hGap
Horizontal gap between columns


vGap

protected int vGap
Vertical gap between rows

Constructor Detail

TableLayout

public TableLayout()
Constructs an instance of TableLayout. This TableLayout will have one row and one column.


TableLayout

public TableLayout(double[][] size)
Constructs an instance of TableLayout.

Parameters:
size - widths of columns and heights of rows in the format, {{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}} If this parameter is invalid, the TableLayout will have exactly one row and one column.
Method Detail

getConstraints

public TableLayoutConstraints getConstraints(java.awt.Component component)
Gets the constraints of a given component.

Parameters:
component - desired component
Returns:
If the given component is found, the constraints associated with that component. If the given component is null or is not found, null is returned.

setConstraints

public void setConstraints(java.awt.Component component,
                           TableLayoutConstraints constraint)
Sets the constraints of a given component.

Parameters:
component - desired component. This parameter cannot be null.
constraint - new set of constraints. This parameter cannot be null.
Returns:
If the given component is found, the constraints associated with that component. If the given component is null or is not found, null is returned.

setColumn

public void setColumn(double[] column)
Adjusts the number and sizes of rows in this layout. After calling this method, the caller should request this layout manager to perform the layout. This can be done with the following code:
     layout.layoutContainer(container);
     container.repaint();
 
or
     window.pack()
 
If this is not done, the changes in the layout will not be seen until the container is resized.

Parameters:
column - heights of each of the columns
See Also:
getColumn

setRow

public void setRow(double[] row)
Adjusts the number and sizes of rows in this layout. After calling this method, the caller should request this layout manager to perform the layout. This can be done with the following code: layout.layoutContainer(container); container.repaint(); or
     window.pack()
 
If this is not done, the changes in the layout will not be seen until the container is resized.

Parameters:
row - widths of each of the rows. This parameter cannot be null.
See Also:
getRow

setCr

protected void setCr(int z,
                     double[] size)
Sets the sizes of rows or columns for the methods setRow or setColumn.

Parameters:
z - indicates row or column

setColumn

public void setColumn(int i,
                      double size)
Adjusts the width of a single column in this layout. After calling this method, the caller should request this layout manager to perform the layout. This can be done with the following code: layout.layoutContainer(container); container.repaint(); or
     window.pack()
 
If this is not done, the changes in the layout will not be seen until the container is resized.

Parameters:
i - zero-based index of column to set. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
size - width of the column. This parameter cannot be null.
See Also:
getColumn

setRow

public void setRow(int i,
                   double size)
Adjusts the height of a single row in this layout. After calling this method, the caller should request this layout manager to perform the layout. This can be done with the following code: layout.layoutContainer(container); container.repaint(); or
     window.pack()
 
If this is not done, the changes in the layout will not be seen until the container is resized.

Parameters:
i - zero-based index of row to set. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
size - height of the row. This parameter cannot be null.
See Also:
getRow

setCr

protected void setCr(int z,
                     int i,
                     double size)
Sets the sizes of rows or columns for the methods setRow or setColumn.

Parameters:
z - indicates row or column
i - indicates which cr to resize

getColumn

public double[] getColumn()
Gets the sizes of columns in this layout.

Returns:
widths of each of the columns
See Also:
setColumn

getRow

public double[] getRow()
Gets the height of a single row in this layout.

Returns:
height of the requested row
See Also:
setRow

getColumn

public double getColumn(int i)
Gets the width of a single column in this layout.

Parameters:
i - zero-based index of row to get. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
Returns:
width of the requested column
See Also:
setRow

getRow

public double getRow(int i)
Gets the sizes of a row in this layout.

Parameters:
i - zero-based index of row to get. If this parameter is not valid, an ArrayOutOfBoundsException will be thrown.
Returns:
height of each of the requested row
See Also:
setRow

getNumColumn

public int getNumColumn()
Gets the number of columns in this layout.

Returns:
the number of columns

getNumRow

public int getNumRow()
Gets the number of rows in this layout.

Returns:
the number of rows

getHGap

public int getHGap()
Gets the horizontal gap between colunns.

Returns:
the horizontal gap in pixels

getVGap

public int getVGap()
Gets the vertical gap between rows.

Returns:
the vertical gap in pixels

setHGap

public void setHGap(int hGap)
Sets the horizontal gap between colunns.

Parameters:
hGap - the horizontal gap in pixels

setVGap

public void setVGap(int vGap)
Sets the vertical gap between rows.

Parameters:
vGap - the horizontal gap in pixels

insertColumn

public void insertColumn(int i,
                         double size)
Inserts a column in this layout. All components to the right of the insertion point are moved right one column. The container will need to be laid out after this method returns. See setColumn.

Parameters:
i - zero-based index at which to insert the column
size - size of the column to be inserted
See Also:
setColumn, deleteColumn

insertRow

public void insertRow(int i,
                      double size)
Inserts a row in this layout. All components below the insertion point are moved down one row. The container will need to be laid out after this method returns. See setRow.

Parameters:
i - zero-based index at which to insert the row
size - size of the row to be inserted
See Also:
setRow, deleteRow

insertCr

public void insertCr(int z,
                     int i,
                     double size)
Inserts a cr for the methods insertRow or insertColumn.

Parameters:
z - indicates row or column
i - zero-based index at which to insert the cr

deleteColumn

public void deleteColumn(int i)
Deletes a column in this layout. All components to the right of the deletion point are moved left one column. The container will need to be laid out after this method returns. See setColumn.

Parameters:
i - zero-based index of column to delete
See Also:
setColumn, deleteColumn

deleteRow

public void deleteRow(int i)
Deletes a row in this layout. All components below the deletion point are moved up one row. The container will need to be laid out after this method returns. See setRow. There must be at least two rows in order to delete a row.

Parameters:
i - zero-based index of row to delete
See Also:
setRow, deleteRow

deleteCr

protected void deleteCr(int z,
                        int i)
Deletes a cr for the methods deleteRow or deleteColumn.

Parameters:
z - indicates row or column
i - zero-based index of cr to delete

toString

public java.lang.String toString()
Converts this TableLayout to a string.

Returns:
a string representing the columns and row sizes in the form "{{col0, col1, col2, ..., colN}, {row0, row1, row2, ..., rowM}}"

getInvalidEntry

public java.awt.Component[] getInvalidEntry()
Determines whether or not there are any components with invalid constraints. An invalid constraint is one that references a non-existing row or column. For example, on a table with five rows, row -1 and row 5 are both invalid. Valid rows are 0 through 4, inclusively.

Returns:
an array of zero or more Component instances
See Also:
getOverlappingEntry

getOverlappingEntry

public java.awt.Component[] getOverlappingEntry()
Gets a list of overlapping components. Two components overlap if they cover at least one common cell.

Returns:
a list of zero or more Component instances
See Also:
getInvalidEntry

calculateSize

protected void calculateSize(java.awt.Container container)
Calculates the sizes of the rows and columns based on the absolute and relative sizes specified in crSpec[R] and crSpec[C] and the size of the container. The result is stored in crSize[R] and crSize[C].

Parameters:
container - container using this TableLayout

assignAbsoluteSize

protected int assignAbsoluteSize(int z,
                                 int availableSize)
Assigns absolute sizes.

Parameters:
z - indicates row or column
availableSize - amount of space available in the container
Returns:
the amount of space available after absolute crs have been assigned sizes

assignRelativeSize

protected int assignRelativeSize(int z,
                                 int availableSize)
Assigns relative sizes.

Parameters:
z - indicates row or column
availableSize - amount of space available in the container
Returns:
the amount of space available after relative crs have been assigned sizes

assignFillSize

protected void assignFillSize(int z,
                              int availableSize)
Assigns FILL sizes.

Parameters:
z - indicates row or column
availableSize - amount of space available in the container
Returns:
the amount of space available after fill crs have been assigned sizes

calculateOffset

protected void calculateOffset(int z,
                               java.awt.Insets inset)
Calculates the offset of each cr.

Parameters:
z - indicates row or column

assignPrefMinSize

protected int assignPrefMinSize(int z,
                                int availableSize,
                                double typeOfSize)
Assigned widths to preferred and minimum size columns and rows. This reduces the available width and height. Minimum widths/heights must be calculated first because they affect preferred widths/heights, but not vice versa. The end result is that any component contained wholly or partly in a column/row of minimum/preferred width will get at least its minimum/preferred width, respectively.

Parameters:
z - indicates row or column
availableSize - amount of space available in the container
typeOfSize - indicates preferred or minimum
Returns:
the amount of space available after absolute crs have been assigned sizes

layoutContainer

public void layoutContainer(java.awt.Container container)
To lay out the specified container using this layout. This method reshapes the components in the specified target container in order to satisfy the constraints of all components.

User code should not have to call this method directly.

Specified by:
layoutContainer in interface java.awt.LayoutManager
Parameters:
container - container being served by this layout manager

calculateSizeAndOffset

protected int[] calculateSizeAndOffset(TableLayout.Entry entry,
                                       int preferredSize,
                                       boolean isColumn)
Calculates the vertical/horizontal offset and size of a component.

Parameters:
entry - entry containing component and contraints
preferredSize - previously calculated preferred width/height of component
isColumn - if true, this method is being called to calculate the offset/size of a column. if false,... of a row.
Returns:
an array, a, of two integers such that a[0] is the offset and a[1] is the size

preferredLayoutSize

public java.awt.Dimension preferredLayoutSize(java.awt.Container container)
Determines the preferred size of the container argument using this layout. The preferred size is the smallest size that, if used for the container's size, will ensure that all components are at least as large as their preferred size. This method cannot guarantee that all components will be their preferred size. For example, if component A and component B are each allocate half of the container's width and component A wants to be 10 pixels wide while component B wants to be 100 pixels wide, they cannot both be accommodated. Since in general components rather be larger than their preferred size instead of smaller, component B's request will be fulfilled. The preferred size of the container would be 200 pixels.

Specified by:
preferredLayoutSize in interface java.awt.LayoutManager
Parameters:
container - container being served by this layout manager
Returns:
a dimension indicating the container's preferred size

minimumLayoutSize

public java.awt.Dimension minimumLayoutSize(java.awt.Container container)
Determines the minimum size of the container argument using this layout. The minimum size is the smallest size that, if used for the container's size, will ensure that all components are at least as large as their minimum size. This method cannot guarantee that all components will be their minimum size. For example, if component A and component B are each allocate half of the container's width and component A wants to be 10 pixels wide while component B wants to be 100 pixels wide, they cannot both be accommodated. Since in general components rather be larger than their minimum size instead of smaller, component B's request will be fulfilled. The minimum size of the container would be 200 pixels.

Specified by:
minimumLayoutSize in interface java.awt.LayoutManager
Parameters:
container - container being served by this layout manager
Returns:
a dimension indicating the container's minimum size

calculateLayoutSize

protected java.awt.Dimension calculateLayoutSize(java.awt.Container container,
                                                 double typeOfSize)
Calculates the preferred or minimum size for the methods preferredLayoutSize and minimumLayoutSize.

Parameters:
container - container whose size is being calculated
typeOfSize - indicates preferred or minimum
Returns:
a dimension indicating the container's preferred or minimum size

calculateLayoutSize

protected int calculateLayoutSize(java.awt.Container container,
                                  int z,
                                  double typeOfSize,
                                  TableLayout.Entry[] entryList,
                                  java.awt.Dimension[] prefMinSize)
Calculates the preferred or minimum size for the method calculateLayoutSize(Container container, double typeOfSize). This method is passed the preferred/minimum sizes of the components so that the potentially expensive methods getPreferredSize()/getMinimumSize() are not called twice for the same component.

Parameters:
container - container whose size is being calculated
typeOfSize - indicates preferred or minimum
entryList - list of Entry objects
prefMinSize - list of preferred or minimum sizes
Returns:
a dimension indicating the container's preferred or minimum size

addLayoutComponent

public void addLayoutComponent(java.lang.String name,
                               java.awt.Component component)
Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager
Parameters:
name - indicates entry's position and anchor
component - component to add

addLayoutComponent

public void addLayoutComponent(java.awt.Component component,
                               java.lang.Object constraint)
Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager2
Parameters:
component - component to add
constraint - indicates entry's position and alignment

removeLayoutComponent

public void removeLayoutComponent(java.awt.Component component)
Removes the specified component from the layout.

Specified by:
removeLayoutComponent in interface java.awt.LayoutManager
Parameters:
component - component being removed

maximumLayoutSize

public java.awt.Dimension maximumLayoutSize(java.awt.Container target)
Returns the maximum dimensions for this layout given the components in the specified target container.

Specified by:
maximumLayoutSize in interface java.awt.LayoutManager2
Parameters:
target - the component which needs to be laid out
Returns:
unconditionally, a Dimension of Integer.MAX_VALUE by Integer.MAX_VALUE since TableLayout does not limit the maximum size of a container

getLayoutAlignmentX

public float getLayoutAlignmentX(java.awt.Container parent)
Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentX in interface java.awt.LayoutManager2
Returns:
unconditionally, 0.5

getLayoutAlignmentY

public float getLayoutAlignmentY(java.awt.Container parent)
Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentY in interface java.awt.LayoutManager2
Returns:
unconditionally, 0.5

invalidateLayout

public void invalidateLayout(java.awt.Container target)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

Specified by:
invalidateLayout in interface java.awt.LayoutManager2

main

public static void main(java.lang.String[] args)