Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
com.lowagie.text.Rectangle
com.lowagie.text.Table
Table
is a Rectangle
that contains Cell
s,
ordered in some kind of matrix.
Tables that span multiple pages are cut into different parts automatically.
If you want a table header to be repeated on every page, you may not forget to
mark the end of the header section by using the method endHeaders()
.
The matrix of a table is not necessarily an m x n-matrix. It can contain holes
or cells that are bigger than the unit. Believe me or not, but it took some serious
thinking to make this as userfriendly as possible. I hope you wil find the result
quite simple (I love simple solutions, especially for complex problems).
I didn't want it to be something as complex as the Java GridBagLayout
.
Example:
The result of this code is a table:// Remark: You MUST know the number of columns when constructing a Table. // The number of rows is not important. Table table = new Table(3); table.setBorderWidth(1); table.setBorderColor(new Color(0, 0, 255)); table.setPadding(5); table.setSpacing(5); Cell cell = new Cell("header"); cell.setHeader(true); cell.setColspan(3); table.addCell(cell); table.endHeaders(); cell = new Cell("example cell with colspan 1 and rowspan 2"); cell.setRowspan(2); cell.setBorderColor(new Color(255, 0, 0)); table.addCell(cell); table.addCell("1.1"); table.addCell("2.1"); table.addCell("1.2"); table.addCell("2.2"); table.addCell("cell test1"); cell = new Cell("big cell"); cell.setRowspan(2); cell.setColspan(2); table.addCell(cell); table.addCell("cell test2");
header | ||
---|---|---|
example cell with colspan 1 and rowspan 2 | 1.1 | 2.1 |
1.2 | 2.2 | |
cell test1 | big cell | |
cell test2 |
Field Summary | |
private String |
|
private int |
|
protected Hashtable |
|
private float |
|
(package private) boolean |
|
private float |
|
private int |
|
private Point | |
private Cell |
|
private int |
|
(package private) boolean |
|
(package private) boolean |
|
(package private) boolean |
|
(package private) float |
|
private ArrayList |
|
(package private) boolean |
|
private static DecimalFormat | |
private float |
|
private float[] |
|
Fields inherited from class com.lowagie.text.Rectangle | |
BOTTOM , BOX , LEFT , NO_BORDER , RIGHT , TOP , UNDEFINED , background , border , borderColorBottom , borderColorLeft , borderColorRight , borderColorTop , borderWidth , borderWidthBottom , borderWidthLeft , borderWidthRight , borderWidthTop , color , grayFill , llx , lly , markupAttributes , rotation , urx , ury , useVariableBorders |
Fields inherited from interface com.lowagie.text.Element | |
ALIGN_BASELINE , ALIGN_BOTTOM , ALIGN_CENTER , ALIGN_JUSTIFIED , ALIGN_JUSTIFIED_ALL , ALIGN_LEFT , ALIGN_MIDDLE , ALIGN_RIGHT , ALIGN_TOP , ALIGN_UNDEFINED , ANCHOR , ANNOTATION , AUTHOR , CCITTG3_1D , CCITTG3_2D , CCITTG4 , CCITT_BLACKIS1 , CCITT_ENCODEDBYTEALIGN , CCITT_ENDOFBLOCK , CCITT_ENDOFLINE , CELL , CHAPTER , CHUNK , CREATIONDATE , CREATOR , GRAPHIC , HEADER , IMGRAW , IMGTEMPLATE , JPEG , KEYWORDS , LIST , LISTITEM , MULTI_COLUMN_TEXT , PARAGRAPH , PHRASE , PRODUCER , PTABLE , RECTANGLE , ROW , SECTION , SUBJECT , TABLE , TITLE |
Constructor Summary | |
| |
| |
|
Method Summary | |
String |
|
void |
|
void |
|
void | |
void | |
void | |
void | |
void | |
void |
|
int |
|
private void |
|
float |
|
float |
|
float |
|
float |
|
int |
|
void |
|
private static double |
|
void |
|
void |
|
boolean |
|
boolean |
|
int |
|
private void |
|
int |
|
ArrayList |
|
Dimension |
|
Object |
|
int |
|
int |
|
float |
|
float[] |
|
float[] |
|
boolean |
|
boolean |
|
void |
|
void |
|
void |
|
static boolean |
|
private boolean |
|
Iterator |
|
int |
|
float |
|
float |
|
private void |
|
private void | |
boolean |
|
float |
|
float |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
private void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
int |
|
float |
|
float |
|
int |
|
float |
|
private String absWidth
This is the width of the table (in pixels).
private int alignment
This is the horizontal alignment.
protected Hashtable alternatingRowAttributes
contains the attributes that are added to each odd (or even) row
private float cellpadding
This is cellpadding.
(package private) boolean cellsFitPage
If true cells may not be split over two pages.
private float cellspacing
This is cellspacing.
private int columns
This is the number of columns in theTable
.
private Point curPosition
private Cell defaultLayout
This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content).
private int lastHeaderRow
This is the number of the last row of the table headers.
(package private) boolean mAutoFillEmptyCells
Boolean to automatically fill empty cells before a table is rendered (takes CPU so may be set to false in case of certainty)
(package private) boolean mDebug
Boolean to track errors (some checks will be performed)
(package private) boolean mTableInserted
Boolean to track if a table was inserted (to avoid unnecessary computations afterwards)
(package private) float offset
This is the offset of the table.
private ArrayList rows
This is the list ofRow
s.
(package private) boolean tableFitsPage
If true this table may not be split over two pages.
private static DecimalFormat widthFormat
private float widthPercentage
This is the width of the table (in percent of the available space).
private float[] widths
This is an array containing the widths (in percentages) of every column.
public Table(Properties attributes)
Returns aTable
that has been constructed taking in account the value of some attributes.
- Parameters:
attributes
- Some attributes
public Table(int columns) throws BadElementException
Constructs aTable
with a certain number of columns.
- Parameters:
columns
- The number of columns in the table
- Throws:
BadElementException
- if the creator was called with less than 1 column
public Table(int columns, int rows) throws BadElementException
Constructs aTable
with a certain number of columns and a certain number ofRow
s.
- Parameters:
columns
- The number of columns in the tablerows
- The number of rows
- Throws:
BadElementException
- if the creator was called with less than 1 column
public String absWidth()
Gets the table width (in pixels).
- Returns:
- the table width
public void addCell(String content) throws BadElementException
Adds aCell
to theTable
. This is a shortcut foraddCell(Cell cell)
. TheString
will be converted to aCell
.
- Parameters:
content
- aString
- Throws:
BadElementException
- this should never happen
public void addCell(String content, Point location) throws BadElementException
Adds aCell
to theTable
. This is a shortcut foraddCell(Cell cell, Point location)
. TheString
will be converted to aCell
.
- Parameters:
content
- aString
location
- aPoint
- Throws:
BadElementException
- this should never happen
public void addCell(Cell aCell, Point aLocation) throws BadElementException
Adds aCell
to theTable
at a certain location.
- Parameters:
aCell
- TheCell
to addaLocation
- The location where theCell
will be added
- Throws:
BadElementException
-
public void addCell(Cell aCell, int row, int column) throws BadElementException
Adds aCell
to theTable
at a certain row and column.
- Parameters:
aCell
- TheCell
to addrow
- The row where theCell
will be addedcolumn
- The column where theCell
will be added
- Throws:
BadElementException
-
public void addCell(Phrase content) throws BadElementException
Adds aCell
to theTable
. This is a shortcut foraddCell(Cell cell)
. ThePhrase
will be converted to aCell
.
- Parameters:
content
- aPhrase
- Throws:
BadElementException
- this should never happen
public void addCell(Phrase content, Point location) throws BadElementException
Adds aCell
to theTable
. This is a shortcut foraddCell(Cell cell, Point location)
. ThePhrase
will be converted to aCell
.
- Parameters:
content
- aPhrase
location
- aPoint
- Throws:
BadElementException
- this should never happen
public void addColumns(int aColumns)
Gives you the posibility to add columns.
- Parameters:
aColumns
- the number of columns to add
public int alignment()
Gets the horizontal alignment.
- Returns:
- a value
private void assumeTableDefaults(Cell aCell)
Sets the unset cell properties to be the table defaults.
- Parameters:
aCell
- The cell to set to table defaults as necessary.
public float bottom()
This method throws anUnsupportedOperationException
.
- Returns:
- NA
public float bottom(int margin)
This method throws anUnsupportedOperationException
.
- Parameters:
margin
- NA
- Returns:
- NA
public float cellpadding()
Gets the cellpadding.
- Returns:
- a value
public float cellspacing()
Gets the cellspacing.
- Returns:
- a value
public int columns()
Gets the number of columns.
- Returns:
- a value
public void complete()
Will fill empty cells with valid blankCell
s
private static final double convertWidth(double val)
public void deleteAllRows()
Deletes all rows in this table. (contributed by dperezcar@fcc.es)
public void deleteColumn(int column) throws BadElementException
Deletes a column in this table.
- Parameters:
column
- the number of the column that has to be deleted
- Throws:
BadElementException
-
public boolean deleteLastRow()
Deletes the last row in this table.
- Returns:
- boolean
true
if the row was deleted;false
if not
public boolean deleteRow(int row)
Deletes a row.
- Parameters:
row
- the number of the row to delete
- Returns:
- boolean
true
if the row was deleted;false
if not
public int endHeaders()
Marks the last row of the table headers.
- Returns:
- the number of the last row of the table headers
private void fillEmptyMatrixCells()
adds newCell
's to empty/null spaces.
public int firstDataRow()
Gets the first number of the row that doesn't contain headers.
- Returns:
- a rownumber
public ArrayList getChunks()
Gets all the chunks in this element.
- Returns:
- an
ArrayList
public Dimension getDimension()
Gets the dimension of this table
- Returns:
- dimension
public Object getElement(int row, int column)
returns the element at the position row, column (Cast to Cell or Table)
- Parameters:
row
-column
-
- Returns:
- dimension
public int getNextColumn()
Returns the next column 0-based index where a new cell would be added. (contributed by dperezcar@fcc.es)
- Returns:
- y coordinate for the next row
public int getNextRow()
Returns the next row 0-based index where a new cell would be added. (contributed by dperezcar@fcc.es)
- Returns:
- x coordinate for the next row
public float getOffset()
Gets the offset of this table.
- Returns:
- the space between this table and the previous element.
public float[] getProportionalWidths()
Gets the proportional widths of the columns in thisTable
.
- Returns:
- the proportional widths of the columns in this
Table
public float[] getWidths(float left, float totalWidth)
Gets an array with the positions of the borders between every column. This method translates the widths expressed in percentages into the x-coordinate of the borders of the columns on a real document.
- Parameters:
left
- this is the position of the first border at the left (cellpadding not included)totalWidth
- this is the space between the first border at the left and the last border at the right (cellpadding not included)
- Returns:
- an array with borderpositions
public boolean hasToFitPageCells()
Checks if the cells of thisTable
have to fit a page.
- Returns:
- true if the cells may not be split
public boolean hasToFitPageTable()
Checks if thisTable
has to fit a page.
- Returns:
- true if the table may not be split
public void insertTable(Table aTable)
To put a table within the existing table at the current position generateTable will of course re-arrange the widths of the columns.
- Parameters:
aTable
- the table you want to insert
public void insertTable(Table aTable, Point aLocation)
To put a table within the existing table at the given position generateTable will of course re-arrange the widths of the columns.
- Parameters:
aTable
- the table you want to insertaLocation
- aPoint
public void insertTable(Table aTable, int row, int column)
To put a table within the existing table at the given position generateTable will of course re-arrange the widths of the columns.
- Parameters:
aTable
- TheTable
to addrow
- The row where theCell
will be addedcolumn
- The column where theCell
will be added
public static boolean isTag(String tag)
Checks if a given tag corresponds with this object.
- Parameters:
tag
- the given tag
- Returns:
- true if the tag corresponds
private boolean isValidLocation(Cell aCell, Point aLocation)
check ifCell
'fits' the table.
- rowspan/colspan not beyond borders
- spanned cell don't overlap existing cells
- Parameters:
aCell
- the cell that has to be checkedaLocation
- the location where the cell has to be placed
- Returns:
- true if the location was valid
public Iterator iterator()
Gets anIterator
of all theRow
s.
- Returns:
- an
Iterator
public int lastHeaderRow()
Gets the last number of the rows that contain headers.
- Returns:
- a rownumber
public float left()
This method throws anUnsupportedOperationException
.
- Returns:
- NA
public float left(int margin)
This method throws anUnsupportedOperationException
.
- Parameters:
margin
- NA
- Returns:
- NA
private void mergeInsertedTables()
Integrates all added tables and recalculates column widths.
private void placeCell(ArrayList someRows, Cell aCell, Point aPosition)
Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.
- Parameters:
someRows
- some rowsaCell
- the cell that has to be insertedaPosition
- the position where the cell has to be placed
public boolean process(ElementListener listener)
Processes the element by adding it (or the different parts) to anElementListener
.
- Parameters:
listener
- anElementListener
- Returns:
true
if the element was processed successfully
public float right()
This method throws anUnsupportedOperationException
.
- Returns:
- NA
public float right(int margin)
This method throws anUnsupportedOperationException
.
- Parameters:
margin
- NA
- Returns:
- NA
public void setAbsWidth(String width)
Sets the width of this table (in percentage of the available space).
- Parameters:
width
- the width
public void setAlignment(String alignment)
Sets the alignment of this paragraph.
- Parameters:
alignment
- the new alignment as aString
public void setAlignment(int value)
Sets the horizontal alignment.
- Parameters:
value
- the new value
public void setAlternatingRowAttribute(String name, String value0, String value1)
Allows clients to set up alternating attributes for each Row in the Table. This code was contributed by Matt Benson.
- Parameters:
name
- the name of the attributevalue0
- the value of the attribute for even rowsvalue1
- the value of the attribute for odd rows
public void setAutoFillEmptyCells(boolean aDoAutoFill)
Enables/disables automatic insertion of empty cells before table is rendered. (default = false) As some people may want to create a table, fill only a couple of the cells and don't bother with investigating which empty ones need to be added, this default behaviour may be very welcome. Disabling is recommended to increase speed. (empty cells should be added through extra code then)
- Parameters:
aDoAutoFill
- enable/disable autofill
public void setBottom(int value)
This method throws anUnsupportedOperationException
.
- Parameters:
value
- NA
public void setCellpadding(float value)
Deprecated. use setSpacing instead
Sets the cellspacing (the meaning of cellpadding and cellspacing was inverted by mistake).
- Parameters:
value
- the new value
public void setCellsFitPage(boolean fitPage)
Allows you to control when a page break occurs. When a cell doesn't fit a page, it is split in two parts. If you want to avoid this, you should set the cellsFitPage value to true.
- Parameters:
fitPage
- enter true if you don't want to split cells
public void setCellspacing(float value)
Deprecated. use setPadding instead
Sets the cellpadding (the meaning of cellpadding and cellspacing was inverted by mistake).
- Parameters:
value
- the new value
private void setCurrentLocationToNextValidPosition(Point aLocation)
Sets current col/row to valid(empty) pos after addCell/Table
- Parameters:
aLocation
- a location in the Table
public void setDebug(boolean aDebug)
Performs extra checks when executing table code (currently only when cells are added).
- Parameters:
aDebug
-
public void setDefaultCellBackgroundColor(Color color)
Changes the backgroundcolor in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
color
- the new color
public void setDefaultCellBorder(int value)
Changes the border in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new border value
public void setDefaultCellBorderColor(Color color)
Changes the bordercolor in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
color
- the new color
public void setDefaultCellBorderWidth(float value)
Changes the width of the borders in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new width
public void setDefaultCellGrayFill(float value)
Changes the grayfill in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new value
public void setDefaultColspan(int value)
Changes the colspan in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new colspan value
public void setDefaultHorizontalAlignment(int value)
Changes the horizontalAlignment in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new alignment value
public void setDefaultLayout(Cell value)
Sets the default layout of the Table to the provided Cell
- Parameters:
value
- a cell with all the defaults
public void setDefaultRowspan(int value)
Changes the rowspan in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new rowspan value
public void setDefaultVerticalAlignment(int value)
Changes the verticalAlignment in the default layout of theCell
s added with methodaddCell(String content)
.
- Parameters:
value
- the new alignment value
public void setLastHeaderRow(int value)
Sets the horizontal alignment.
- Parameters:
value
- the new value
public void setLeft(int value)
This method throws anUnsupportedOperationException
.
- Parameters:
value
- NA
public void setOffset(float offset)
Sets the offset of this table. Normally a newline is added before you add a Table object. This newline uses the current leading. If you want to control the space between the table and the previous element yourself, you have to set the offset of this table.
- Parameters:
offset
- the space between this table and the previous object.
public void setPadding(float value)
Sets the cellpadding.
- Parameters:
value
- the new value
public void setRight(int value)
This method throws anUnsupportedOperationException
.
- Parameters:
value
- NA
public void setSpaceBetweenCells(float value)
Sets the cellspacing.
- Parameters:
value
- the new value
public void setSpaceInsideCell(float value)
Sets the cellpadding.
- Parameters:
value
- the new value
public void setSpacing(float value)
Sets the cellspacing.
- Parameters:
value
- the new value
public void setTableFitsPage(boolean fitPage)
Allows you to control when a page break occurs. When a table doesn't fit a page, it is split in two parts. If you want to avoid this, you should set the tableFitsPage value to true.
- Parameters:
fitPage
- enter true if you don't want to split cells
public void setTop(int value)
This method throws anUnsupportedOperationException
.
- Parameters:
value
- NA
public void setWidth(float width)
Sets the width of this table (in percentage of the available space).
- Parameters:
width
- the width
public void setWidths(float[] widths) throws BadElementException
Sets the widths of the different columns (percentages). You can give up relative values of borderwidths. The sum of these values will be considered 100%. The values will be recalculated as percentages of this sum. example:The widths will be: a width of 50% for the first column, 25% for the second and third column.float[] widths = {2, 1, 1}; table.setWidths(widths)
- Parameters:
widths
- an array with values
- Throws:
BadElementException
-
public void setWidths(int[] widths) throws DocumentException
Sets the widths of the different columns (percentages). You can give up relative values of borderwidths. The sum of these values will be considered 100%. The values will be recalculated as percentages of this sum.
- Parameters:
widths
- an array with values
- Throws:
DocumentException
-
public int size()
Gets the number of rows in thisTable
.
- Returns:
- the number of rows in this
Table
public float top()
This method throws anUnsupportedOperationException
.
- Returns:
- NA
public float top(int margin)
This method throws anUnsupportedOperationException
.
- Parameters:
margin
- NA
- Returns:
- NA
public int type()
Gets the type of the text element.
- Returns:
- a type
public float widthPercentage()
Gets the table width (a percentage).
- Returns:
- the table width