FileDocCategorySizeDatePackage
SmartGridLayout.javaAPI DocJava SE 5 API4225Fri Aug 26 14:58:00 BST 2005javax.swing.colorchooser

SmartGridLayout

public class SmartGridLayout extends Object implements Serializable, LayoutManager
A better GridLayout class
version
1.8 12/19/03
author
Steve Wilson

Fields Summary
int
rows
int
columns
int
xGap
int
yGap
int
componentCount
Component[]
layoutGrid
Constructors Summary
public SmartGridLayout(int numColumns, int numRows)



       
    rows = numRows;
    columns = numColumns;
    layoutGrid = new Component[numColumns][numRows];

  
Methods Summary
public voidaddLayoutComponent(java.lang.String s, java.awt.Component c)

private voidbuildLayoutGrid(java.awt.Container c)


      Component[] children = c.getComponents();

      for (int componentCount = 0; componentCount < children.length; componentCount++) {
	//	System.out.println("Children: " +componentCount);
	int row = 0;
	int column = 0;

	if (componentCount != 0) {
	  column = componentCount % columns;
	  row = (componentCount - column) / columns;
	}

	//	System.out.println("inserting into: "+ column +  " " + row);
      
	layoutGrid[column][row] = children[componentCount];
      }
  
private intcomputeColumnWidth(int columnNum)

    int maxWidth = 1;
    for (int row = 0; row < rows; row++) {
      int width = layoutGrid[columnNum][row].getPreferredSize().width;
      if (width > maxWidth) {
	maxWidth = width;
      }
    }
    return maxWidth;
  
private intcomputeRowHeight(int rowNum)

    int maxHeight = 1;
    for (int column = 0; column < columns; column++) {
      int height = layoutGrid[column][rowNum].getPreferredSize().height;
      if (height > maxHeight) {
	maxHeight = height;
      }
    }
    return maxHeight;
  
public voidlayoutContainer(java.awt.Container c)


    buildLayoutGrid(c);

    int[] rowHeights = new int[rows];
    int[] columnWidths = new int[columns];
    
    for (int row = 0; row < rows; row++) {
        rowHeights[row] = computeRowHeight(row);
    }

    for (int column = 0; column < columns; column++) {
        columnWidths[column] = computeColumnWidth(column);
    }


    Insets insets = c.getInsets();

    if (c.getComponentOrientation().isLeftToRight()) {
        int horizLoc = insets.left;
        for (int column = 0; column < columns; column++) {
          int vertLoc = insets.top;
      
          for (int row = 0; row < rows; row++) {
            Component current = layoutGrid[column][row];

            current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
            //	System.out.println(current.getBounds());
            vertLoc += (rowHeights[row] + yGap);
          }
          horizLoc += (columnWidths[column] + xGap );
        }
    } else {  
        int horizLoc = c.getWidth() - insets.right;
        for (int column = 0; column < columns; column++) {
          int vertLoc = insets.top;
          horizLoc -= columnWidths[column];
      
          for (int row = 0; row < rows; row++) {
            Component current = layoutGrid[column][row];

            current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
            //	System.out.println(current.getBounds());
            vertLoc += (rowHeights[row] + yGap);
          }
          horizLoc -= xGap;
        }
    }
      


  
public java.awt.DimensionminimumLayoutSize(java.awt.Container c)


    buildLayoutGrid(c);
    Insets insets = c.getInsets();
    


    int height = 0;
    int width = 0;
    
    for (int row = 0; row < rows; row++) {
        height += computeRowHeight(row);
    }

    for (int column = 0; column < columns; column++) {
        width += computeColumnWidth(column);
    }

    height += (yGap * (rows - 1)) + insets.top + insets.bottom;
    width += (xGap * (columns - 1)) + insets.right + insets.left;

    return new Dimension(width, height);


  
public java.awt.DimensionpreferredLayoutSize(java.awt.Container c)

      return minimumLayoutSize(c);
  
public voidremoveLayoutComponent(java.awt.Component c)