|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--layout.TableLayout
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. If the component occupies more than one cell, the component is resized to fit perfectly in the rectangular region of cells. If the component occupies a single cell, it can be aligned in four ways within that cell.
A single cell component can be stretched horizontally to fit the cell (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 in the vertical.
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(); }
Inner Class Summary | |
protected class |
TableLayout.Entry
|
Field Summary | |
protected int[] |
columnOffset
Offsets of columns in pixels. |
protected int[] |
columnSize
Widths of columns in pixels |
protected double[] |
columnSpec
Widths of columns 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 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 int[] |
rowOffset
Offsets of rows in pixels. |
protected int[] |
rowSize
Heights of rows in pixels |
protected double[] |
rowSpec
Heights of rows expressed in absolute and relative terms |
Fields inherited from interface layout.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 void |
calculateSize(java.awt.Container container)
Calculates the sizes of the rows and columns based on the absolute and relative sizes specified in rowSpec and columnSpec
and the size of the container. |
void |
deleteColumn(int i)
Deletes a column in this layout. |
void |
deleteRow(int i)
Deletes a row in this layout. |
void |
drawGrid(java.awt.Container container,
java.awt.Graphics g)
Draws a grid on the given container. |
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. |
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. |
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. |
boolean |
hidden()
Determines whether or not there are any hidden components. |
void |
insertColumn(int i,
double size)
Inserts a column in this layout. |
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. |
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. |
boolean |
overlapping()
Determines whether or not there are any overlapping components. |
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. |
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. |
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 |
protected static final double[][] defaultSize
protected double[] columnSpec
protected double[] rowSpec
protected int[] columnSize
protected int[] rowSize
protected int[] columnOffset
protected int[] rowOffset
protected java.util.LinkedList list
protected boolean dirty
protected int oldWidth
protected int oldHeight
Constructor Detail |
public TableLayout()
public TableLayout(double[][] size)
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 |
public TableLayoutConstraints getConstraints(java.awt.Component component)
component
- desired componentpublic void setConstraints(java.awt.Component component, TableLayoutConstraints constraint)
component
- desired component. This parameter cannot be null.constraint
- new set of constraints. This parameter cannot be null.public void setColumn(double[] column)
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.
column
- heights of each of the columnsgetColumn
public void setRow(double[] row)
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.
row
- widths of each of the rows. This parameter cannot be null.getRow
public void setColumn(int i, double size)
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.
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.getColumn
public void setRow(int i, double size)
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.
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.getRow
public double[] getColumn()
setColumn
public double[] getRow()
setRow
public double getColumn(int i)
i
- zero-based index of row to get. If this parameter is not valid,
an ArrayOutOfBoundsException will be thrown.setRow
public double getRow(int i)
i
- zero-based index of row to get. If this parameter is not valid,
an ArrayOutOfBoundsException will be thrown.setRow
public int getNumColumn()
public int getNumRow()
public void insertColumn(int i, double size)
setColumn
.i
- zero-based index at which to insert the column.size
- size of the column to be insertedsetColumn
,
deleteColumn
public void insertRow(int i, double size)
setRow
.i
- zero-based index at which to insert the column.size
- size of the row to be insertedsetRow
,
deleteRow
public void deleteColumn(int i)
setColumn
.i
- zero-based index of column to deletesetColumn
,
deleteColumn
public void deleteRow(int i)
setRow
. There must be at least two rows in order
to delete a row.i
- zero-based index of column to deletesetRow
,
deleteRow
public java.lang.String toString()
toString
in class java.lang.Object
public void drawGrid(java.awt.Container container, java.awt.Graphics g)
container
- container using this TableLayoutg
- graphics content of container (can be offscreen)public boolean hidden()
overlapping
public boolean overlapping()
hidden
protected void calculateSize(java.awt.Container container)
rowSpec
and columnSpec
and the size of the container. The result is stored in rowSize
and columnSize
.container
- container using this TableLayoutpublic void layoutContainer(java.awt.Container container)
User code should not have to call this method directly.
layoutContainer
in interface java.awt.LayoutManager
container
- container being served by this layout managerpublic java.awt.Dimension preferredLayoutSize(java.awt.Container container)
preferredLayoutSize
in interface java.awt.LayoutManager
container
- container being served by this layout managerpublic java.awt.Dimension minimumLayoutSize(java.awt.Container container)
minimumLayoutSize
in interface java.awt.LayoutManager
container
- container being served by this layout managerpublic void addLayoutComponent(java.lang.String name, java.awt.Component component)
addLayoutComponent
in interface java.awt.LayoutManager
name
- indicates entry's position and anchorcomponent
- component to addpublic void addLayoutComponent(java.awt.Component component, java.lang.Object constraint)
addLayoutComponent
in interface java.awt.LayoutManager2
component
- component to addconstraint
- indicates entry's position and alignmentpublic void removeLayoutComponent(java.awt.Component component)
removeLayoutComponent
in interface java.awt.LayoutManager
component
- component being removedpublic java.awt.Dimension maximumLayoutSize(java.awt.Container target)
maximumLayoutSize
in interface java.awt.LayoutManager2
target
- the component which needs to be laid outpublic float getLayoutAlignmentX(java.awt.Container parent)
getLayoutAlignmentX
in interface java.awt.LayoutManager2
public float getLayoutAlignmentY(java.awt.Container parent)
getLayoutAlignmentY
in interface java.awt.LayoutManager2
public void invalidateLayout(java.awt.Container target)
invalidateLayout
in interface java.awt.LayoutManager2
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |