Class Table
- All Implemented Interfaces:
Serializable
,Cloneable
,TableModel
This data table class is implemented as a subclass of
AbstractTableModel
so that it can be displayed
directly in a JTable
.
- Since:
- 2006.09.16
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
read/write mode: read/write only marked columns (non-negative)static final int
read mode: only adapt types, but do not load datastatic final int
read mode: do not add new columns (no extension)static final int
read/write mode: no header present/do not write a headerstatic final int
read mode: do not allow null valuesstatic final int
read/write mode: verbose message output (print tuple counter)static final int
read/write mode: tuple weight in last field (currently unused)Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Abort an i/o operation.Add a column to the table.int
Add a column to the table.void
autoDir()
Automatically determine the direction of columns.void
autoType()
Automatically determine the type of columns.void
clear()
Clear this table, that is, remove all rows.void
Clear the internal record buffer.clone()
Clone this table.void
Clone all columns' type and data arrays and replace originals.void
Clone all columns' types and replace originals.void
Clone all columns' types and replace originals.void
cloneColumn
(int index) Clone a column's type and value array and replace originals.Clone the domain descriptions of this table.int
findColumn
(String name) Get the index of a column given its name.getColumn
(int index) Get a column given its index.Get a column given its name.Class<?>
getColumnClass
(int col) Get the value class of a column given its index.int
Get the number of columns of the table.int
getColumnDir
(int col) Get the direction of a column given its index.int
getColumnIndex
(String name) Get the index of a column given its name.getColumnName
(int col) Get the name of a column given its index.getColumnType
(int col) Get the type of a column given its index.double
getColumnWeight
(int col) Get the weight of a column given its index.getName()
Get the name of the table.int
Get the number of rows of the table.getStringAt
(int row, int col) Get the value of a table cell as a string.getValueAt
(int row, int col) Get the value of a table cell as an object.boolean
isCellEditable
(int row, int col) Returns whether a table cell is editable.static void
Main function for testing basic functionality.void
moveColumn
(int src, int dst) Move a column to a new position.static Table
Parse a table description.int
read
(util.TableReader reader) Read table from an input stream.int
read
(util.TableReader reader, int mode) Read a table from an input stream.void
readHeader
(util.TableReader reader) Read table header from an input stream.void
readHeader
(util.TableReader reader, int mode) Read table header from an input stream.boolean
readRow
(int row, util.TableReader reader) Read a table row.boolean
readRow
(int row, util.TableReader reader, int mode) Read a table row.boolean
readRow
(util.TableReader reader) Read a table row.void
removeColumn
(int index) Remove a column from the table.void
removeColumn
(String name) Remove a column from the table.int
renameColumn
(int index, String name) Rename a column, that is, give it a new name.void
reorderColumns
(int[] map) Reorder the columns of the table.void
resize
(int newcnt) Resize the table, that is, change the number of rows.void
setColumnDir
(int col, int dir) Set the direction of a column given its index.void
setColumnType
(int col, ColType type) Set the type of a column given its index.void
setColumnWeight
(int col, double wgt) Set the weight of a column given its index.void
Set the name of the table.void
setValueAt
(Object value, int row, int col) Set the value of a table cell from an object.int[]
Sort the columns of the table by name.int[]
sortColumns
(Comparator<Object> cmp) Sort the columns of the table.void
Sort the values of the nominal columns.void
sortTypes
(Comparator<Object> cmp) Sort the values of the nominal columns.toString()
Create a string description of the table type.Create a string description of the table type.void
write
(util.TableWriter writer) Write the table.void
write
(util.TableWriter writer, int mode) Write the table.void
writeHeader
(util.TableWriter writer) Write a table header.void
writeHeader
(util.TableWriter writer, int mode) Write a table header.void
writeRow
(int row, util.TableWriter writer) Write a table row.void
writeRow
(int row, util.TableWriter writer, int mode) Write a table row.Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
-
Field Details
-
NOHEADER
public static final int NOHEADERread/write mode: no header present/do not write a header- See Also:
-
NODATA
public static final int NODATAread mode: only adapt types, but do not load data- See Also:
-
NOEXTEND
public static final int NOEXTENDread mode: do not add new columns (no extension)- See Also:
-
NONULLS
public static final int NONULLSread mode: do not allow null values- See Also:
-
MARKED
public static final int MARKEDread/write mode: read/write only marked columns (non-negative)- See Also:
-
WEIGHT
public static final int WEIGHTread/write mode: tuple weight in last field (currently unused)- See Also:
-
VERBOSE
public static final int VERBOSEread/write mode: verbose message output (print tuple counter)- See Also:
-
-
Constructor Details
-
Table
public Table()Create a data table.- Since:
- 2007.04.13 (Christian Borgelt)
-
Table
Create a data table.- Parameters:
name
- the name of the table- Since:
- 2006.09.16 (Christian Borgelt)
-
-
Method Details
-
clone
Clone this table.The clone is not a fully deep copy, as it keeps the types and the data arrays of the columns. However, the structure managing the column organization (order, naming etc) is independent of the old table and thus it is possible to rename/reorder columns and to add new columns without affecting the original table.
If a deeper copy of the table is needed, the table should first be cloned with this function (i.e.
clone()
) and then the clone deepened withcloneAllColumns()
,cloneAllTypes()
orcloneAllData()
. -
cloneDomains
Clone the domain descriptions of this table.- Returns:
- a clone of this table without the data
- Since:
- 2007.05.15 (Christian Borgelt)
-
cloneColumn
public void cloneColumn(int index) Clone a column's type and value array and replace originals.With this method the shallow copy of the table as returned by
clone()
can be deepened for particular columns, when changes to the type and the contents of a column become necessary and the state of the original column must be preserved.- Parameters:
index
- the index of the column to clone- Since:
- 2006.10.06 (Christian Borgelt)
-
cloneAllColumns
public void cloneAllColumns()Clone all columns' type and data arrays and replace originals.With this method the shallow copy of the table as returned by
clone()
can be fully deepened.- Since:
- 2006.10.06 (Christian Borgelt)
-
cloneAllTypes
public void cloneAllTypes()Clone all columns' types and replace originals.With this method the shallow copy of the table as returned by
clone()
can be partially deepened.- Since:
- 2005.05.02 (Christian Borgelt)
-
cloneAllData
public void cloneAllData()Clone all columns' types and replace originals.With this method the shallow copy of the table as returned by
clone()
can be partially deepened.- Since:
- 2005.05.02 (Christian Borgelt)
-
clear
public void clear()Clear this table, that is, remove all rows.- Since:
- 2006.10.04 (Christian Borgelt)
-
getName
Get the name of the table.- Returns:
- the name of the table
- Since:
- 2006.10.04 (Christian Borgelt)
-
setName
Set the name of the table.- Parameters:
name
- the new name of the table- Since:
- 2006.10.04 (Christian Borgelt)
-
getRowCount
public int getRowCount()Get the number of rows of the table.- Specified by:
getRowCount
in interfaceTableModel
- Returns:
- the number of rows of the table
- Since:
- 2006.10.04 (Christian Borgelt)
-
getColumnCount
public int getColumnCount()Get the number of columns of the table.- Specified by:
getColumnCount
in interfaceTableModel
- Returns:
- the number of columns of the table
- Since:
- 2006.09.16 (Christian Borgelt)
-
getColumn
Get a column given its index.- Parameters:
index
- the index of the column to get- Returns:
- the column with the given index
- Since:
- 2006.10.04 (Christian Borgelt)
-
getColumn
Get a column given its name.- Parameters:
name
- the index of the column to get- Returns:
- the column with the given name
- Since:
- 2006.10.04 (Christian Borgelt)
-
getColumnName
Get the name of a column given its index.- Specified by:
getColumnName
in interfaceTableModel
- Overrides:
getColumnName
in classAbstractTableModel
- Parameters:
col
- the index of the column- Returns:
- the name of the column with the given index
- Since:
- 2006.10.04 (Christian Borgelt)
-
getColumnIndex
Get the index of a column given its name.This function is equivalent to
findColumn()
.- Parameters:
name
- the name of the column- Returns:
- the index of the column or
-1
if there is no column with the given name - Since:
- 2006.10.04 (Christian Borgelt)
- See Also:
-
findColumn
Get the index of a column given its name.This function is equivalent to
getColumnIndex()
.- Overrides:
findColumn
in classAbstractTableModel
- Parameters:
name
- the name of the column- Returns:
- the index of the column or
-1
if there is no column with the given name - Since:
- 2006.10.04 (Christian Borgelt)
- See Also:
-
getColumnType
Get the type of a column given its index.- Parameters:
col
- the index of the column- Returns:
- the type of the column with the given index
- Since:
- 2007.06.07 (Christian Borgelt)
-
setColumnType
Set the type of a column given its index.- Parameters:
col
- the index of the columntype
- the type to set- Since:
- 2007.07.19 (Christian Borgelt)
-
getColumnClass
Get the value class of a column given its index.- Specified by:
getColumnClass
in interfaceTableModel
- Overrides:
getColumnClass
in classAbstractTableModel
- Parameters:
col
- the index of the column- Returns:
- the value class of the column with the given index
- Since:
- 2007.03.18 (Christian Borgelt)
-
getColumnDir
public int getColumnDir(int col) Get the direction of a column given its index.- Parameters:
col
- the index of the column- Returns:
- the direction of the column
- Since:
- 2007.07.19 (Christian Borgelt)
-
setColumnDir
public void setColumnDir(int col, int dir) Set the direction of a column given its index.- Parameters:
col
- the index of the columndir
- the direction to set- Since:
- 2007.07.19 (Christian Borgelt)
-
getColumnWeight
public double getColumnWeight(int col) Get the weight of a column given its index.- Parameters:
col
- the index of the column- Returns:
- the weight of the column
- Since:
- 2007.07.24 (Christian Borgelt)
-
setColumnWeight
public void setColumnWeight(int col, double wgt) Set the weight of a column given its index.- Parameters:
col
- the index of the columnwgt
- the weight to set- Since:
- 2007.07.24 (Christian Borgelt)
-
addColumn
Add a column to the table.It is assumed that the column has the same number of rows as any column already present in the table. The first column added to a table determines the table's number of rows.
- Parameters:
col
- the column to add- Returns:
- the index of the added column or
-1
if a column with the same name already exists - Since:
- 2006.09.16 (Christian Borgelt)
-
addColumn
Add a column to the table.- Parameters:
name
- the name of the column to addtype
- the type of the column to add- Returns:
- the added column or
null
if a column with the same name already exists - Since:
- 2007.07.23 (Christian Borgelt)
-
renameColumn
Rename a column, that is, give it a new name.- Parameters:
index
- the index of the columnname
- the new name of the column- Returns:
- the index of the column, that is,
index
or-1
if replacing would create a duplicate - Since:
- 2006.10.06 (Christian Borgelt)
-
removeColumn
public void removeColumn(int index) Remove a column from the table.- Parameters:
index
- the index of the column to remove- Since:
- 2007.01.31 (Christian Borgelt)
-
removeColumn
Remove a column from the table.- Parameters:
name
- the name of the column to remove- Since:
- 2007.01.31 (Christian Borgelt)
-
moveColumn
public void moveColumn(int src, int dst) Move a column to a new position.- Parameters:
src
- the index of the column to movedst
- the index to which to move the column- Since:
- 2007.01.31 (Christian Borgelt)
-
reorderColumns
public void reorderColumns(int[] map) Reorder the columns of the table.The desired reordering has to be stated as a permutation of the integer numbers 0 to
size()-1
, with each entry stating the new index for the column that is identified by the array index (forward map).If the reordering map is not such a permutation, the table will get into an inconsistent state that may lead to serious errors.
- Parameters:
map
- an integer array containing a permutation of the integer numbers 0 togetColumnCount()-1
- Since:
- 2007.01.31 (Christian Borgelt)
-
sortColumns
public int[] sortColumns()Sort the columns of the table by name.- Returns:
- a map from the old indices (array indices) to the new indices (contents of array elements)
- Since:
- 2007.07.26 (Christian Borgelt)
-
sortColumns
Sort the columns of the table.- Parameters:
cmp
- the comparator for the columns- Returns:
- a map from the old indices (array indices) to the new indices (contents of array elements)
- Since:
- 2007.07.26 (Christian Borgelt)
-
autoType
public void autoType()Automatically determine the type of columns.Note that it is only tried to convert nominal columns.
- Since:
- 2007.02.02 (Christian Borgelt)
-
autoDir
public void autoDir()Automatically determine the direction of columns.- Since:
- 2007.06.07 (Christian Borgelt)
-
sortTypes
public void sortTypes()Sort the values of the nominal columns.- Since:
- 2007.07.26 (Christian Borgelt)
-
sortTypes
Sort the values of the nominal columns.- Parameters:
cmp
- the comparator for the values- Since:
- 2007.07.26 (Christian Borgelt)
-
isCellEditable
public boolean isCellEditable(int row, int col) Returns whether a table cell is editable.Editing is currently not supported, therefore this function always returns
false
.- Specified by:
isCellEditable
in interfaceTableModel
- Overrides:
isCellEditable
in classAbstractTableModel
- Parameters:
row
- the row of the cell to accesscol
- the column of the cell to access- Returns:
- whether the specified cell is editable
- Since:
- 2006.10.04 (Christian Borgelt)
-
getValueAt
Get the value of a table cell as an object.- Specified by:
getValueAt
in interfaceTableModel
- Parameters:
row
- the row of the cell to accesscol
- the column of the cell to access- Returns:
- an object representing the value in the specified cell
- Since:
- 2006.10.04 (Christian Borgelt)
-
setValueAt
Set the value of a table cell from an object.- Specified by:
setValueAt
in interfaceTableModel
- Overrides:
setValueAt
in classAbstractTableModel
- Parameters:
value
- the value to set in the specified cellrow
- the row of the cell to setcol
- the column of the cell to set- Since:
- 2006.10.04 (Christian Borgelt)
-
getStringAt
Get the value of a table cell as a string.- Parameters:
row
- the row of the cell to accesscol
- the column of the cell to access- Returns:
- a string description of the value in the specified cell
- Since:
- 2006.10.04 (Christian Borgelt)
-
resize
public void resize(int newcnt) Resize the table, that is, change the number of rows.Note that new value arrays are allocated regardless of whether the new number of rows coincides with the old number or not. Hence calling this function with the old number of rows effectively clones the value arrays of all columns.
- Parameters:
newcnt
- the new number of rows- Since:
- 2007.01.31 (Christian Borgelt)
-
readHeader
Read table header from an input stream.- Parameters:
reader
- the table reader to read from- Throws:
IOException
- if a read error occurs- Since:
- 2007.05.15 (Christian Borgelt)
- See Also:
-
readHeader
Read table header from an input stream.If
(mode & NOHEADER) != 0
, default field names are generated and the first data record is read into an internal buffer, which is processed on the next call to any of thereadRow()
functions.- Parameters:
reader
- the table reader to read frommode
- the read mode- Throws:
IOException
- if a read error occurs- Since:
- 2007.05.15 (Christian Borgelt)
-
clearBuffer
public void clearBuffer()Clear the internal record buffer.The internal record buffer of a table gets filled if one of the
readHeader()
functions is called with(mode & NOHEADER) != 0
. This internal buffer would be processed on the next call to one of thereadRow()
functions. If this is not desired, the internal buffer can be cleared by calling this function.- Since:
- 2007.05.21 (Christian Borgelt)
-
readRow
Read a table row.The row is not stored in the table, only the column types are adapted.
- Parameters:
reader
- the table reader to read from- Returns:
- whether a record was read
- Throws:
IOException
- if a read error occurs- Since:
- 2007.05.15 (Christian Borgelt)
- See Also:
-
readRow
Read a table row.If
row < 0
, the row is not stored in the table, only the column types are adapted. Ifrow ≥ 0
, the corresponding table row must already exist in the table (that is, it must begetRowCount() > row
).- Parameters:
row
- the table row in which to store the read recordreader
- the table reader to read from- Returns:
- whether a record was read
- Throws:
IOException
- if a read error occurs- Since:
- 2007.05.15 (Christian Borgelt)
- See Also:
-
readRow
Read a table row.If
row < 0
, the row is not stored in the table, only the column types are adapted. Ifrow ≥ 0
, the corresponding table row must already exist in the table (that is, it must begetRowCount() > row
).- Parameters:
row
- the table row in which to store the recordreader
- the table reader to read frommode
- the read mode- Returns:
- whether a record was read
- Throws:
IOException
- if a read error occurs- Since:
- 2007.05.15 (Christian Borgelt)
-
read
Read table from an input stream.- Parameters:
reader
- the table reader to read from- Returns:
- the number of rows read
- Throws:
IOException
- if a read error occurs- Since:
- 2007.01.31 (Christian Borgelt)
-
read
Read a table from an input stream.- Parameters:
reader
- the table reader to read frommode
- the read mode- Returns:
- the number of rows read
- Throws:
IOException
- if a read error occurs- Since:
- 2007.02.08 (Christian Borgelt)
-
writeHeader
Write a table header.- Parameters:
writer
- the table writer- Throws:
IOException
- if a write error occurs- Since:
- 2007.05.15 (Christian Borgelt)
-
writeHeader
Write a table header.- Parameters:
writer
- the table writermode
- the write mode- Throws:
IOException
- if a write error occurs- Since:
- 2007.05.15 (Christian Borgelt)
-
writeRow
Write a table row.- Parameters:
row
- the table row to writewriter
- the table writer- Throws:
IOException
- if a write error occurs- Since:
- 2007.05.19 (Christian Borgelt)
-
writeRow
Write a table row.- Parameters:
row
- the table row to writewriter
- the writer to write the table row tomode
- the write mode- Throws:
IOException
- if a write error occurs- Since:
- 2007.05.19 (Christian Borgelt)
-
write
Write the table.- Parameters:
writer
- the table writer- Throws:
IOException
- if a write error occurs- Since:
- 2007.01.31 (Christian Borgelt)
-
write
Write the table.- Parameters:
writer
- the writer to write the table tomode
- the write mode- Throws:
IOException
- if a write error occurs- Since:
- 2007.01.31 (Christian Borgelt)
-
abort
public void abort()Abort an i/o operation.Reading or writing a table with one of the
read()
orwrite()
functions, respectively, is aborted after the next table row.- Since:
- 2007.05.08 (Christian Borgelt)
-
toString
Create a string description of the table type.- Parameters:
header
- the text to add as a header- Returns:
- a string description of the table type
- Since:
- 2007.05.02 (Christian Borgelt)
-
toString
Create a string description of the table type. -
parse
Parse a table description.- Parameters:
name
- the name of the table to createscan
- the scanner to read from- Returns:
- the described table
- Throws:
IOException
- if an i/o error occurs or a duplicate column name is found- Since:
- 2007.02.16 (Christian Borgelt)
-
main
Main function for testing basic functionality.- Parameters:
args
- the command line arguments- Since:
- 2004.06.02 (Christian Borgelt)
-