Class TableLayout
- All Implemented Interfaces:
TableLayoutConstants
,LayoutManager
,LayoutManager2
,Serializable
public class TableLayout extends Object implements LayoutManager2, Serializable, TableLayoutConstants
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(); }
- Author:
- Daniel E. Barbalace
- See Also:
- Serialized Form
-
Field Summary
-
Constructor Summary
Constructors Constructor Description TableLayout()
Constructs an instance of TableLayout.TableLayout(double[][] size)
Constructs an instance of TableLayout. -
Method Summary
Modifier and Type Method Description void
addLayoutComponent(Component component, Object constraint)
Adds the specified component with the specified name to the layout.void
addLayoutComponent(String name, Component component)
Adds the specified component with the specified name to the layout.void
deleteColumn(int i)
Deletes a column in this layout.void
deleteRow(int i)
Deletes a row in this layout.void
drawGrid(Container container, Graphics g)
Draws a grid on the given container.static JComponent
get3Split(JComponent left, JComponent middle, JComponent right, double sizeLeft, double sizeMiddle, double sizeRight)
static JComponent
get3Split(JComponent left, JComponent middle, JComponent right, double sizeLeft, double sizeMiddle, double sizeRight, double spaceBetween, double border)
static JComponent
get3SplitVertical(JComponent top, JComponent middle, JComponent bottom, double sizeTop, double sizeMiddle, double sizeBottom)
static JComponent
get3SplitVertical(JComponent top, JComponent middle, JComponent bottom, double sizeTop, double sizeMiddle, double sizeBottom, double spaceBetween, double border)
static JComponent
get4Split(JComponent left, JComponent middle1, JComponent middle2, JComponent right, double width, double spaceBetween, double border)
static JComponent
get4Split(JComponent left, JComponent middle1, JComponent middle2, JComponent right, double w1, double w2, double w3, double w4, double spaceBetween, double border)
static JComponent
get4SplitVertical(JComponent top, JComponent middle1, JComponent middle2, JComponent bottom, double sizeTop, double sizeMiddle1, double sizeMiddle2, double sizeBottom, double spaceBetween, double border)
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(Component component)
Gets the constraints of a given component.static JComponent
getDoubleRow(JComponent label, JComponent row2, Color color)
static LayoutManager
getLayout(double[] width, double height)
static LayoutManager
getLayout(double width, double height)
static LayoutManager
getLayout(double width, double[] height)
static LayoutManager
getLayout(double width, double height, int columns, int rows)
static LayoutManager
getLayout(int width, int height)
float
getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis.float
getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis.static JComponent
getLeftDivider(JComponent gui, double layout, Color color, int width)
static JComponent
getMultiSplit(ArrayList<JComponent> guiElements)
static JComponent
getMultiSplit(ArrayList<JComponent> guiElements, double componentsWidth, int spaceT, int spaceL, int spaceB, int spaceR)
static JComponent
getMultiSplitVertical(Collection<?> jComponentList)
static JComponent
getMultiSplitVertical(Collection<?> jComponentList, double width, int spaceBetween)
static JComponent
getMultiSplitVertical(Collection<?> jComponentList, int spaceBetween)
static JPanel
getMultiSplitVerticalNonPrefHeight(Collection<?> jComponentList, double h)
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.static JComponent
getSplit(JComponent leftComponent, JComponent rightComponent, double leftSize, double rightSize)
static JPanel
getSplitVertical(Component topComponent, Component bottomComponent, double topSize, double bottomSize)
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(Container target)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.void
layoutContainer(Container container)
To lay out the specified container using this layout.Dimension
maximumLayoutSize(Container target)
Returns the maximum dimensions for this layout given the components in the specified target container.Dimension
minimumLayoutSize(Container container)
Determines the minimum size of the container argument using this layout.boolean
overlapping()
Determines whether or not there are any overlapping components.Dimension
preferredLayoutSize(Container container)
Determines the preferred size of the container argument using this layout.void
removeLayoutComponent(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(Component component, TableLayoutConstraints constraint)
Sets the constraints of a given component.static void
setMultiSplitVerticalNonPrefHeight_Contents(Collection<?> jComponentList, double h, JPanel result)
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.String
toString()
Converts this TableLayout to a string.
-
Constructor Details
-
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 Details
-
getConstraints
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
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.
-
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();
orwindow.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();
orwindow.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()
-
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();
orwindow.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();
orwindow.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()
-
getColumn
public double[] getColumn()Gets the sizes of columns in this layout.- Returns:
- widths of each of the columns
- See Also:
setColumn(double[])
-
getRow
public double[] getRow()Gets the height of a single row in this layout.- Returns:
- height of the requested row
- See Also:
setRow(double[])
-
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(double[])
-
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(double[])
-
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
-
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. SeesetColumn
.- Parameters:
i
- zero-based index at which to insert the column.size
- size of the column to be inserted- See Also:
setColumn(double[])
,deleteColumn(int)
-
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. SeesetRow
.- Parameters:
i
- zero-based index at which to insert the column.size
- size of the row to be inserted- See Also:
setRow(double[])
,deleteRow(int)
-
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. SeesetColumn
.- Parameters:
i
- zero-based index of column to delete- See Also:
setColumn(double[])
,deleteColumn(int)
-
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. SeesetRow
. There must be at least two rows in order to delete a row.- Parameters:
i
- zero-based index of column to delete- See Also:
setRow(double[])
,deleteRow(int)
-
toString
Converts this TableLayout to a string. -
drawGrid
Draws a grid on the given container. This is useful for seeing where the rows and columns go. In the container's paint method, call this method.- Parameters:
container
- container using this TableLayoutg
- graphics content of container (can be offscreen)
-
overlapping
public boolean overlapping()Determines whether or not there are any overlapping components. Two components overlap if they cover at least one common cell.- Returns:
- True, if there are any overlapping components. False, otherwise.
- See Also:
hidden()
-
layoutContainer
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 interfaceLayoutManager
- Parameters:
container
- container being served by this layout manager
-
preferredLayoutSize
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 interfaceLayoutManager
- Parameters:
container
- container being served by this layout manager- Returns:
- a dimension indicating the container's preferred size
-
minimumLayoutSize
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 interfaceLayoutManager
- Parameters:
container
- container being served by this layout manager- Returns:
- a dimension indicating the container's minimum size
-
addLayoutComponent
Adds the specified component with the specified name to the layout.- Specified by:
addLayoutComponent
in interfaceLayoutManager
- Parameters:
name
- indicates entry's position and anchorcomponent
- component to add
-
addLayoutComponent
Adds the specified component with the specified name to the layout.- Specified by:
addLayoutComponent
in interfaceLayoutManager2
- Parameters:
component
- component to addconstraint
- indicates entry's position and alignment
-
removeLayoutComponent
Removes the specified component from the layout.- Specified by:
removeLayoutComponent
in interfaceLayoutManager
- Parameters:
component
- component being removed
-
maximumLayoutSize
Returns the maximum dimensions for this layout given the components in the specified target container.- Specified by:
maximumLayoutSize
in interfaceLayoutManager2
- 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
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 interfaceLayoutManager2
- Returns:
- unconditionally, 0.5
-
getLayoutAlignmentY
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 farthest away from the origin, 0.5 is centered, etc.- Specified by:
getLayoutAlignmentY
in interfaceLayoutManager2
- Returns:
- unconditionally, 0.5
-
invalidateLayout
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.- Specified by:
invalidateLayout
in interfaceLayoutManager2
-
getSplit
public static JComponent getSplit(JComponent leftComponent, JComponent rightComponent, double leftSize, double rightSize) -
getSplitVertical
-
getDoubleRow
-
get3Split
public static JComponent get3Split(JComponent left, JComponent middle, JComponent right, double sizeLeft, double sizeMiddle, double sizeRight) -
get3Split
public static JComponent get3Split(JComponent left, JComponent middle, JComponent right, double sizeLeft, double sizeMiddle, double sizeRight, double spaceBetween, double border) -
get4Split
public static JComponent get4Split(JComponent left, JComponent middle1, JComponent middle2, JComponent right, double width, double spaceBetween, double border) -
get4Split
public static JComponent get4Split(JComponent left, JComponent middle1, JComponent middle2, JComponent right, double w1, double w2, double w3, double w4, double spaceBetween, double border) -
get3SplitVertical
public static JComponent get3SplitVertical(JComponent top, JComponent middle, JComponent bottom, double sizeTop, double sizeMiddle, double sizeBottom) -
get3SplitVertical
public static JComponent get3SplitVertical(JComponent top, JComponent middle, JComponent bottom, double sizeTop, double sizeMiddle, double sizeBottom, double spaceBetween, double border) -
get4SplitVertical
public static JComponent get4SplitVertical(JComponent top, JComponent middle1, JComponent middle2, JComponent bottom, double sizeTop, double sizeMiddle1, double sizeMiddle2, double sizeBottom, double spaceBetween, double border) -
getLayout
-
getLayout
-
getLayout
-
getLayout
-
getMultiSplit
-
getMultiSplit
public static JComponent getMultiSplit(ArrayList<JComponent> guiElements, double componentsWidth, int spaceT, int spaceL, int spaceB, int spaceR) -
getMultiSplitVertical
-
getMultiSplitVerticalNonPrefHeight
-
setMultiSplitVerticalNonPrefHeight_Contents
public static void setMultiSplitVerticalNonPrefHeight_Contents(Collection<?> jComponentList, double h, JPanel result) -
getMultiSplitVertical
-
getMultiSplitVertical
public static JComponent getMultiSplitVertical(Collection<?> jComponentList, double width, int spaceBetween) -
getLeftDivider
-
getLayout
-