|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.gargoylesoftware.base.gui.TableLayout
public class TableLayout
The TableLayout lays out items based on a table of rows and columns.
If you are doing simple layout, you can specify constraints as strings of the format "row,column". If you want the component to stretch over multiple rows/columns then you can specify the constraint as "row+rowspan,column+columnspan" as shown in the sample below.
final TableLayout layout = new TableLayout(); final JPanel panel = new JPanel(layout); panel.add( new JLabel("squirrel"), "1,1" ); panel.add( new JLabel("raccoon"), "1,2" ); panel.add( new JLabel("bluejay"), "2,1" ); panel.add( new JLabel("goldfish"), "2,2" ); panel.add( new JLabel("marshhawk"), "3,1+3" );If you want more flexibility over the layout then this, use a
TableLayoutConstraints
object instead of a string. Here is a more complicated sample that uses
TableLayoutConstraints
to customize the layout a bit more. Note the use of
TableLayoutDebuggingPanel
- this will draw lines on layout boundaries to help
debug layout problems.
final TableLayout layout = new TableLayout(); final JPanel panel = new TableLayoutDebuggingPanel(layout); TableLayoutConstraints constraints; layout.setRowExpandable(1, true); constraints = new TableLayoutConstraints(1,1); constraints.setVerticalStretch(true); panel.add( new JButton("squirrel"), constraints ); constraints = new TableLayoutConstraints(1,2); constraints.setVerticalAlignment(TableLayout.TOP); panel.add( new JButton("raccoon"), constraints ); panel.add( new JButton("bluejay"), "2,1" ); panel.add( new JButton("goldfish"), "2,2" ); panel.add( new JButton("marshhawk"), "3,1+3" );Debugging tip: Most layout problems become obvious if you use a
TableLayoutDebuggingPanel
to see where the layout boundaries are. In those
rare cases where this doesn't give you enough information, try calling
setTraceChannel(TraceChannel)
with a non-null TraceChannel such as Trace.out
or Trace.err. This will dump quite a bit of diagnostic information.
layout.setTraceChannel(Trace.out)
Nested Class Summary | |
---|---|
private class |
TableLayout.Entry
A convenience class to attach the constraints to a component. |
private class |
TableLayout.Header
A convenience class to hold information specific to a row or column. |
private class |
TableLayout.HeaderPermanentInfo
Contains the information that the user has specified for the specific row or column. |
Field Summary | |
---|---|
private java.awt.Dimension |
actualSize_
|
private int |
columnCount_
|
private java.util.Set |
columnHeaderPermanentInfos_
|
private java.util.List |
constraints_
|
private int |
horizontalAlignment_
|
private boolean |
ignoreInvisibleComponents_
|
private java.awt.Dimension |
maximumSize_
|
private java.awt.Dimension |
minimumSize_
|
private java.awt.Container |
parent_
|
private java.awt.Dimension |
preferredSize_
|
private int |
rowCount_
|
private java.util.Set |
rowHeaderPermanentInfos_
|
private static long |
serialVersionUID
|
private TableLayout.Header[] |
tempColumnHeaders_
|
private TableLayout.Header[] |
tempRowHeaders_
|
private boolean |
tempSizesAreValid_
|
private TraceChannel |
traceChannel_
|
private int |
verticalAlignment_
|
Fields inherited from interface javax.swing.SwingConstants |
---|
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST |
Constructor Summary | |
---|---|
TableLayout()
Create a new TableLayout. |
Method Summary | |
---|---|
void |
addLayoutComponent(java.awt.Component comp,
java.lang.Object constraints)
Add the specified component to the layout with the specified constraints. |
void |
addLayoutComponent(java.lang.String name,
java.awt.Component comp)
Add the specified component to the layout with the specified constraints. |
private void |
adjustHeaderSizes(TableLayout.Header[] sizes)
Fix up all the headers such that minimum <= preferred <= maximum |
private void |
adjustSizesForSpanning(int start,
int span,
TableLayout.Header[] sizes,
int minSize,
int preferredSize,
int maxSize)
Adjust the various sizes to account for components than span multiple columns/rows. |
private boolean |
areAnyExpandable(java.util.Set permanentInfos)
Return true if any of the infos are expandable. |
protected void |
assertNotNull(java.lang.String fieldName,
java.lang.Object fieldValue)
Verify that the specified value is not null. |
private void |
calculateActualSizes(java.awt.Dimension parentSize)
Calculate the actual sizes to be used based on the actual dimension of the parent container. |
private int |
calculateActualSizes(TableLayout.Header[] sizes,
int preferredLength,
int clipLength)
Calculate the actual sizes for the specified row or column headers. |
private void |
calculateMinMaxPreferredSizes()
Calculate the various sizes. |
private void |
calculatePositions(java.awt.Container parent,
java.awt.Dimension parentSize)
Calculate all the positions of the various rows/columns |
private void |
calculateRowAndColumnCount()
The list of constraints has been modified. |
private void |
calculateSizes()
Calculate all the various sizing information required for this layout. |
void |
drawOutlines(java.awt.Graphics graphics)
A debugging method that draws lines on the parent component to show where the table cell boundaries are. |
private void |
expandToFit(TableLayout.Header[] sizes,
int clipLength)
Expand the specified sizes to fit within the specified clipLength. |
private java.awt.Dimension |
getComponentMaximumSize(java.awt.Component component)
Return the minimum size of the specified component. |
private java.awt.Dimension |
getComponentMinimumSize(java.awt.Component component)
Return the minimum size of the specified component. |
private java.awt.Dimension |
getComponentPreferredSize(java.awt.Component component)
Return the minimum size of the specified component. |
private TableLayoutConstraints |
getConstraints(java.awt.Component component)
Return the TableLayoutConstraints object that corresponds to the specified component or null if this component could not be found. |
private TableLayout.Header[] |
getExpandableHeaders(int first,
int last,
TableLayout.Header[] headers)
Return an array containing all the headers that are expandable. |
int |
getHorizontalAlignment()
Return the horizontal alignment. |
boolean |
getIgnoreInvisibleComponents()
Get whether or not we should ignore an components that are not visible. |
float |
getLayoutAlignmentX(java.awt.Container target)
I don't really understand what this method is supposed to return so I always return 0F. |
float |
getLayoutAlignmentY(java.awt.Container target)
I don't really understand what this method is supposed to return so I always return 0F. |
private int |
getMinimumColumnWidth(int index)
Return the minimum column width. |
private int |
getMinimumRowHeight(int index)
Return the minimum row height. |
private TableLayout.HeaderPermanentInfo |
getPermanentInfo(java.util.Set infoList,
int index,
boolean createIfNeeded)
TODO: Provide comments |
TraceChannel |
getTraceChannel()
Return the trace channel. |
int |
getVerticalAlignment()
Return the vertical alignment. |
private void |
initTempSizes()
Initialize the temporary arrays (tempRowHeaders_ and tempColumnHeaders_) for use in a calculation. |
private void |
invalidateLayout()
Invalidate the layout. |
void |
invalidateLayout(java.awt.Container target)
Invalidate the layout and throw away and temporary calculations. |
boolean |
isColumnExpandable(int index)
Return true if this column can be expanded beyond its preferred size. |
boolean |
isRowExpandable(int index)
Return true if this row can be expanded beyond its preferred size. |
void |
layoutContainer(java.awt.Container parent)
Layout all the components in this container. |
java.awt.Dimension |
maximumLayoutSize(java.awt.Container target)
Return the maximum layout size. |
java.awt.Dimension |
minimumLayoutSize(java.awt.Container parent)
Get the minimum size of this layout. |
private void |
positionComponent(TableLayout.Entry entry,
int x,
int y,
int width,
int height)
Position one component given the bounding coordinates |
java.awt.Dimension |
preferredLayoutSize(java.awt.Container parent)
Return the preferred layout size. |
void |
removeLayoutComponent(java.awt.Component comp)
Remove the specified component from the layout. |
void |
setColumnExpandable(int index,
boolean isExpandable)
Set whether this column can be expanded beyond its preferred size. |
void |
setHorizontalAlignment(int alignment)
Set the vertical alignment. |
void |
setIgnoreInvisibleComponents(boolean ignore)
Set whether or not we should ignore an components that are not visible. |
void |
setMinimumColumnWidth(int index,
int size)
Set the minimum column width for a specific column. |
void |
setMinimumRowHeight(int index,
int size)
Set the minimum row height for a specific row. |
private void |
setParent(java.awt.Container newParent)
Set the parent container for this layout. |
void |
setRowExpandable(int index,
boolean isExpandable)
Set whether this row can be expanded beyond its preferred size. |
void |
setTraceChannel(TraceChannel channel)
Set the trace channel used for printing diagnostic information. |
void |
setVerticalAlignment(int alignment)
Set the vertical alignment. |
private void |
shrinkToFit(TableLayout.Header[] sizes,
int clipLength)
Shrink the specified sizes to fit within the specified clipLength. |
private static java.lang.String |
toString(java.awt.Dimension dimension)
Convenience method to create a string from a Dimension object. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final long serialVersionUID
private final java.util.Set rowHeaderPermanentInfos_
private final java.util.Set columnHeaderPermanentInfos_
private java.awt.Container parent_
private final java.util.List constraints_
private TraceChannel traceChannel_
private TableLayout.Header[] tempColumnHeaders_
private TableLayout.Header[] tempRowHeaders_
private int columnCount_
private int rowCount_
private boolean tempSizesAreValid_
private int verticalAlignment_
private int horizontalAlignment_
private java.awt.Dimension minimumSize_
private java.awt.Dimension maximumSize_
private java.awt.Dimension preferredSize_
private java.awt.Dimension actualSize_
private boolean ignoreInvisibleComponents_
Constructor Detail |
---|
public TableLayout()
Method Detail |
---|
private static java.lang.String toString(java.awt.Dimension dimension)
dimension
- Description of Parameter
public void setVerticalAlignment(int alignment)
alignment
- The new vertical alignment.public void setHorizontalAlignment(int alignment)
alignment
- The new horizontal alignment.public void setMinimumRowHeight(int index, int size)
index
- The row that we are setting the height for.size
- The new minimum height.public void setMinimumColumnWidth(int index, int size)
index
- The column that we are setting the width for.size
- The new width.public void setRowExpandable(int index, boolean isExpandable)
index
- The row index.isExpandable
- true if the row is to be expandable.public void setColumnExpandable(int index, boolean isExpandable)
index
- The column index.isExpandable
- true if the column is to be expandable.public void setTraceChannel(TraceChannel channel)
channel
- The new trace channel.public void setIgnoreInvisibleComponents(boolean ignore)
ignore
- True if we should ignore them.public float getLayoutAlignmentX(java.awt.Container target)
getLayoutAlignmentX
in interface java.awt.LayoutManager2
target
- The container that this layout is managing.
public float getLayoutAlignmentY(java.awt.Container target)
getLayoutAlignmentY
in interface java.awt.LayoutManager2
target
- The container that this layout is managing.
public int getVerticalAlignment()
setVerticalAlignment(int)
public int getHorizontalAlignment()
public boolean isRowExpandable(int index)
index
- The row index
public boolean isColumnExpandable(int index)
index
- The column.
public TraceChannel getTraceChannel()
public boolean getIgnoreInvisibleComponents()
public void addLayoutComponent(java.lang.String name, java.awt.Component comp) throws java.lang.UnsupportedOperationException
addLayoutComponent
in interface java.awt.LayoutManager
name
- The constraints string.comp
- the component that is being
added.
java.lang.UnsupportedOperationException
- If called.public void addLayoutComponent(java.awt.Component comp, java.lang.Object constraints)
addLayoutComponent
in interface java.awt.LayoutManager2
comp
- The component that is being added.constraints
- The constraints object.TableLayoutConstraints.makeConstraints(String)
public void removeLayoutComponent(java.awt.Component comp)
removeLayoutComponent
in interface java.awt.LayoutManager
comp
- The component to remove.public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
minimumLayoutSize
in interface java.awt.LayoutManager
parent
- The container that this layout is managing.
public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
preferredLayoutSize
in interface java.awt.LayoutManager
parent
- The container that this layout is managing.
public void layoutContainer(java.awt.Container parent)
layoutContainer
in interface java.awt.LayoutManager
parent
- The container that this layout is managing.public java.awt.Dimension maximumLayoutSize(java.awt.Container target)
maximumLayoutSize
in interface java.awt.LayoutManager2
target
- The container that this layout is managing.
public void invalidateLayout(java.awt.Container target)
invalidateLayout
in interface java.awt.LayoutManager2
target
- The container that this layout is managing.public void drawOutlines(java.awt.Graphics graphics)
graphics
- The graphics object.TableLayoutDebuggingPanel
private void setParent(java.awt.Container newParent)
newParent
- The new parent valueprivate TableLayout.Header[] getExpandableHeaders(int first, int last, TableLayout.Header[] headers)
first
- Description of Parameterlast
- Description of Parameterheaders
- Description of Parameter
private int getMinimumRowHeight(int index)
index
- Description of Parameter
private int getMinimumColumnWidth(int index)
index
- The column index.
private TableLayout.HeaderPermanentInfo getPermanentInfo(java.util.Set infoList, int index, boolean createIfNeeded)
infoList
- Description of Parameterindex
- Description of ParametercreateIfNeeded
- Description of Parameter
private TableLayoutConstraints getConstraints(java.awt.Component component)
component
- Description of Parameter
private final java.awt.Dimension getComponentMinimumSize(java.awt.Component component)
component
- The component that we will be querying
private final java.awt.Dimension getComponentMaximumSize(java.awt.Component component)
component
- The component that we will be querying
private final java.awt.Dimension getComponentPreferredSize(java.awt.Component component)
component
- The component that we will be querying
private void calculateMinMaxPreferredSizes()
private boolean areAnyExpandable(java.util.Set permanentInfos)
permanentInfos
- The infos.
private void invalidateLayout()
private void calculateSizes()
private void adjustSizesForSpanning(int start, int span, TableLayout.Header[] sizes, int minSize, int preferredSize, int maxSize)
start
- The starting index of the component.span
- The number of columns/rows that the component
spans.sizes
- The headers that we are adjusting.minSize
- The minimum size of the component.preferredSize
- The preferred size of the component.maxSize
- The maximum size of the component.private void calculatePositions(java.awt.Container parent, java.awt.Dimension parentSize)
parent
- Description of ParameterparentSize
- Description of Parameterprivate void positionComponent(TableLayout.Entry entry, int x, int y, int width, int height)
entry
- Description of Parameterx
- Description of Parametery
- Description of Parameterwidth
- Description of Parameterheight
- Description of Parameterprivate void calculateRowAndColumnCount()
private void calculateActualSizes(java.awt.Dimension parentSize)
parentSize
- Description of Parameterprivate void adjustHeaderSizes(TableLayout.Header[] sizes)
sizes
- Description of Parameterprivate int calculateActualSizes(TableLayout.Header[] sizes, int preferredLength, int clipLength)
sizes
- Description of ParameterpreferredLength
- Description of ParameterclipLength
- Description of Parameter
private void expandToFit(TableLayout.Header[] sizes, int clipLength)
sizes
- Description of ParameterclipLength
- Description of Parameterprivate void shrinkToFit(TableLayout.Header[] sizes, int clipLength)
sizes
- Description of ParameterclipLength
- Description of Parameterprivate void initTempSizes()
protected final void assertNotNull(java.lang.String fieldName, java.lang.Object fieldValue) throws DetailedNullPointerException
fieldName
- The name of the field to checkfieldValue
- The value of the field to check
DetailedNullPointerException
- If fieldValue is null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |