|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.swing.table.AbstractTableModel
org.jdesktop.jdnc.table.DefaultTableModelExt
public class DefaultTableModelExt
Class used to represent a tabular data model which holds 2 dimensional
data. This class provides a simple alternative to javax.sql.RowSet
and
is intended for use when the program is not obtaining its data directly
from a JDBC data source. Programs which are interfacing with databases using
JDBC should use the RowSet
API instead.
A tabular data model is structured with a fixed number of columns where
each column is represented by an integer index. The first
column's index is 0 and the last column's index is columnCount - 1. Each
column in the tabular data model has a MetaData
instance which describes the
column's data-type and edit constraints.
The structure of the model (number of columns and associated column meta-data which describes those columns) must be initialized before the rows of data are loaded into the model. The structure can be obtained directly from the data source (if set) or it can be set explicitly by the program. The following example configures the table's structure explicitly:
DefaultTableModelExt data = new DefaultTableModelExt();
data.setColumnCount(3);
MetaData columnMetaData = data.getColumnMetaData(0);
columnMetaData.setName("firstname");
columnMetaData = data.getColumnMetaData(1);
columnMetaData.setName("lastname");
columnMetaData = data.getColumnMetaData(2);
columnMetaData.setName("employeeid");
columnMetaData.setElementClass(Integer.class);
If the meta-data properties for each column are not explicitly set, the type
of each column will default to java.lang.String
.
The data for the model may be loaded either by explicitly invoking the
addRows
or insertRows
method, or by setting the "source" property, in
which case the tabular data model will handle streaming the data in from that
source when startLoading
is called. The format of the data at that
source URL must be readible by the DataLoader instance set as the "loader"
property. By default the loader is set to a TableModelExtTextLoader
instance,
which can read the data in simple ascii format where the columns are
separated by configurable regular expression delimeters. Examples of this format are
tab-separated-values (TSV) and comma-separated-values (CSV). If the data at the
source is coming in an alternate format, the caller is responsible for setting
an appropriate loader object before invoking startLoading
.
If the data for the above example was available from a tsv file, the tabular data model could be loaded with the following code:
data.setSource("file:///D:/acme/employees.tsv");
data.startLoading();
It is often desirable to determine the structure of the tabular data model (number of columns, types, etc) from the source rather than setting it explicitly. The amount of structural information available from a source will depend on the format of the data. The plain text formats do not typically contain meta-data information beyond optional column names encoded as the first row, and thus it is desirable to explicitly configure the column meta-data for these simple formats.
Once the data has been loaded into the model, values at a specific row and column may be retreived or modified. For example:
int rowCount = data.getRowCount();
for (int i = 0; i < rowCount; i++) {
Float payrate = (Float)data.getValueAt(i, 4);
if (payrate < 10.00) {
// underpaid - give raise
data.setValueAt(i, 4, new Float(payrate*.05));
}
}
Additionally, rows can be inserted or deleted. Example:
// hired
Object employee[] = new Object[4];
employee[0] = "Einstein";
employee[1] = "Albert";
employee[2] = new Integer(430);
employee[3] = new Float(12.50);
data.insertRow(5, employee);
// fired
data.deleteRow(6);
This class is also an instance of javax.swing.table.TableModel
and thus
can be set as the model for any javax.swing.JTable
component instance.
DefaultTableModelExt generates table model events when either its structure or
values are modified. To listen for these events, register a TableModelListener
instance. Example:
DefaultTableModelExt data = new DefaultTableModelExt("http://www.foo.com/barQuery");
data.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
// handle tabular data model change
}
});
MetaData
,
TableModel
,
TableModelEvent
,
TableModelListener
,
TableModelExtTextLoader
,
Serialized FormField Summary |
---|
Fields inherited from class javax.swing.table.AbstractTableModel |
---|
listenerList |
Constructor Summary | |
---|---|
DefaultTableModelExt()
Creates a new tabular data model with 0 columns. |
|
DefaultTableModelExt(int columnCount)
Creates a new tabular data model with the specified number of columns. |
|
DefaultTableModelExt(java.lang.String urlName)
Creates a new tabular data model configured to obtain its data in plain text format from the specified URL. |
|
DefaultTableModelExt(java.lang.String urlName,
int columnCount)
Creates a new tabular data model configured to obtain its data in plain text format from the specified URL. |
|
DefaultTableModelExt(java.net.URL sourceURL)
Create a new tabular data model configured to obtain its data in plain text format from the specified URL. |
Method Summary | |
---|---|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener pcl)
Adds the specified property change listener to this tabular data model. |
void |
addRow(java.lang.Object[] row)
Adds a new row at the end of the tabular data model. |
void |
addRows(java.util.List rows)
Adds a collection of new rows at the end of the tabular data model. |
void |
deleteRow(int rowIndex)
Deletes the row at the specified row index from the tabular data model. |
void |
deleteRows(int firstRowIndex,
int lastRowIndex)
Deletes the contiguous set of rows in the specified range from this tabular data model. |
java.lang.Class |
getColumnClass(int columnIndex)
Returns the value of the specified column's meta-data "elementClass" property. |
int |
getColumnCount()
Returns the number of columns in this tabular data model. |
int |
getColumnIndex(java.lang.String columnName)
Note that the columnName parameter must be the "name" of the column as stored in the column's MetaData and should not be confused with the getColumnName method from TableModel, which actually
refers to the "label" property on the MetaData. |
MetaData |
getColumnMetaData(int columnIndex)
Returns the meta-data for the specified column. |
java.lang.String |
getColumnName(int columnIndex)
Returns the value of the specified column's meta-data "label" property. |
int |
getFieldCount()
|
java.lang.String[] |
getFieldNames()
Note: if the type for id is changed to Object type this will have to change to returning Object[]. |
DataLoader |
getLoader()
Returns a TableModelExtTextLoader instance by default, which can read data in plain text format where rows are separated by newlines and columns within each row are separated by the tab character (delimeters are configurable). |
MetaData[] |
getMetaData()
If the data source is non-null and the number of columns has not yet been initialized, this method will attempt to initialize the number of columns by reading any available meta-data from the source. |
MetaData |
getMetaData(java.lang.String columnName)
Note: String will likely be converted to type Object for the ID |
java.beans.PropertyChangeListener[] |
getPropertyChangeListeners()
|
java.lang.Object[] |
getRow(int rowIndex)
Returns the contents of the row at the specified row index. |
int |
getRowCount()
|
java.net.URL |
getSource()
|
java.lang.Object |
getValueAt(int rowIndex,
int columnIndex)
|
void |
insertRow(int rowIndex,
java.lang.Object[] row)
Inserts a new row in the tabular data model at the specified row index. |
void |
insertRows(int rowIndex,
java.util.List rows)
Inserts a collection of new rows into the tabular data model at the specified row index. |
boolean |
isCellEditable(int rowIndex,
int columnIndex)
Returns the negation of the specified column's meta-data "readOnly" property. |
boolean |
isLoading()
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener pcl)
Removes the specified property change listener from this tabular data model. |
void |
setColumnCount(int columnCount)
Initializes the number of columns in this tabular data model and creates new instances of column MetaData objects for each column. |
void |
setColumnMetaData(int columnIndex,
MetaData metaData)
Sets the meta data object for the specified column. |
void |
setLoader(DataLoader loader)
Sets the data loader object to be used to stream in the data from the source URL. |
void |
setSource(java.lang.String urlName)
Sets the "source" URL property after converting the URL string to a URL instance. |
void |
setSource(java.net.URL sourceURL)
Sets the "source" property. |
void |
setValueAt(java.lang.Object value,
int rowIndex,
int columnIndex)
Sets the value of the tabular data element at the specified row and column. |
void |
startLoading()
Starts loading the data asynchronously from the tabular data object's source. |
Methods inherited from class javax.swing.table.AbstractTableModel |
---|
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DefaultTableModelExt()
public DefaultTableModelExt(int columnCount)
columnCount
- integer indicating the number of columns in this
tabular data model
java.lang.IllegalArgumentException
- if columnCount < 0public DefaultTableModelExt(java.lang.String urlName) throws java.net.MalformedURLException
urlName
- String containing the name of the URL which provides the
data for this tabular data model
java.net.MalformedURLException
- if urlName cannot be converted to a valid URLpublic DefaultTableModelExt(java.net.URL sourceURL)
sourceURL
- URL which provides the data for this tabular data modelpublic DefaultTableModelExt(java.lang.String urlName, int columnCount) throws java.net.MalformedURLException
urlName
- String containing the name of the URL which provides the
data for this tabular data modelcolumnCount
- integer indicating the number of columns in this
tabular data model
java.lang.IllegalArgumentException
- if columnCount < 0
java.net.MalformedURLException
- if urlName cannot be converted to a valid URLMethod Detail |
---|
public void setSource(java.lang.String urlName) throws java.net.MalformedURLException
URL
instance.
urlName
- String containing the name of the URL which provides the
data for this tabular data model
java.net.MalformedURLException
- if urlName cannot be converted to a valid URLgetSource()
public void setSource(java.net.URL sourceURL)
sourceURL
- URL which provides the data for this tabular data modelgetSource()
public java.net.URL getSource()
setSource(java.lang.String)
public void setLoader(DataLoader loader)
loader
- DataLoader object which can read data in the format provided
from the sourcegetLoader()
public DataLoader getLoader()
TableModelExtTextLoader
,
setLoader(org.jdesktop.jdnc.DataLoader)
public int getColumnCount()
getColumnCount
in interface javax.swing.table.TableModel
public void setColumnCount(int columnCount)
columnCount
- integer indicating the number of columns in this
tabular data model
java.lang.IllegalArgumentException
- if columnCount < 0public MetaData getColumnMetaData(int columnIndex)
columnIndex
- integer index indicating column in tabular data model
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCountpublic MetaData getMetaData(java.lang.String columnName)
MetaDataProvider
getMetaData
in interface MetaDataProvider
columnName
- String containing the id for the column
public MetaData[] getMetaData()
getMetaData
in interface MetaDataProvider
public java.lang.String[] getFieldNames()
MetaDataProvider
getFieldNames
in interface MetaDataProvider
public int getFieldCount()
getFieldCount
in interface MetaDataProvider
public void setColumnMetaData(int columnIndex, MetaData metaData)
columnIndex
- integer index indicating column in tabular data modelmetaData
- object describing the data element at the specified
column
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCountpublic java.lang.Class getColumnClass(int columnIndex)
getColumnClass
in interface javax.swing.table.TableModel
getColumnClass
in class javax.swing.table.AbstractTableModel
columnIndex
- integer index indicating column in tabular data model
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCountMetaData.getElementClass()
public java.lang.String getColumnName(int columnIndex)
getColumnName
in interface javax.swing.table.TableModel
getColumnName
in class javax.swing.table.AbstractTableModel
columnIndex
- integer index indicating column in tabular data model
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCountMetaData.getLabel()
public boolean isCellEditable(int rowIndex, int columnIndex)
isCellEditable
in interface javax.swing.table.TableModel
isCellEditable
in class javax.swing.table.AbstractTableModel
rowIndex
- integer index indicating row in tabular data modelcolumnIndex
- integer index indicating column in tabular data model
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCount or
rowIndex < 0 or rowIndex >= rowCountMetaData.isReadOnly()
public int getRowCount()
getRowCount
in interface javax.swing.table.TableModel
public java.lang.Object getValueAt(int rowIndex, int columnIndex)
getValueAt
in interface javax.swing.table.TableModel
rowIndex
- integer index indicating row in tabular data modelcolumnIndex
- integer index indicating column in tabular data model
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCount or
rowIndex < 0 or rowIndex >= rowCountpublic void setValueAt(java.lang.Object value, int rowIndex, int columnIndex)
setValueAt
in interface javax.swing.table.TableModel
setValueAt
in class javax.swing.table.AbstractTableModel
value
- Object containing value of tabular data elementrowIndex
- integer index indicating row in tabular data modelcolumnIndex
- integer index indicating column in tabular data model
java.lang.RuntimeException
- if the column is not editable
java.lang.IndexOutOfBoundsException
- if
columnIndex < 0 or columnIndex >= columnCount or
rowIndex < 0 or rowIndex >= rowCountgetColumnClass(int)
,
isCellEditable(int, int)
public void deleteRow(int rowIndex)
rowIndex
- integer index indicating row in tabular data model
java.lang.IndexOutOfBoundsException
- if
rowIndex < 0 or rowIndex >= rowCountpublic void deleteRows(int firstRowIndex, int lastRowIndex)
firstRowIndex
- integer index indicating first row in rangelastRowIndex
- integer index indicating last row in range
java.lang.IllegalArgumentException
- if lastRowIndex < firstRowIndex
java.lang.IndexOutOfBoundsException
- if
firstRowIndex < 0 or firstRowIndex >= rowCount or
lastRowIndex < 0 or lastRowIndex >= rowCountpublic void insertRow(int rowIndex, java.lang.Object[] row)
rowIndex
- integer index indicating row in tabular data modelrow
- array containing the column elements for the row
java.lang.IndexOutOfBoundsException
- if
rowIndex < 0 or rowIndex >= rowCountpublic void insertRows(int rowIndex, java.util.List rows)
rowIndex
- integer index indicating row in tabular data modelrows
- list containing an object array for each row to be added
java.lang.IndexOutOfBoundsException
- if
rowIndex < 0 or rowIndex >= rowCountpublic void addRow(java.lang.Object[] row)
row
- array containing the column elements for the rowpublic void addRows(java.util.List rows)
rows
- list containing an object array for each row to be addedpublic void startLoading() throws java.io.IOException
java.lang.IllegalStateException
- if the "source" property is null
java.io.IOException
public boolean isLoading()
startLoading()
public java.lang.Object[] getRow(int rowIndex)
setValueAt
to change the contents of the tabular data
model.
rowIndex
- integer index indicating row in tabular data model
java.lang.IndexOutOfBoundsException
- if
rowIndex < 0 or rowIndex >= rowCountsetValueAt(java.lang.Object, int, int)
public int getColumnIndex(java.lang.String columnName)
getColumnName
method from TableModel, which actually
refers to the "label" property on the MetaData.
columnName
- String containing the name of the column
java.lang.IllegalArgumentException
- if the column name does not exist in
this tabular data modelMetaData.getName()
public void addPropertyChangeListener(java.beans.PropertyChangeListener pcl)
pcl
- PropertyChangeListener object to receive events when
tabular data model properties changepublic void removePropertyChangeListener(java.beans.PropertyChangeListener pcl)
pcl
- PropertyChangeListener object to receive events when
tabular data model properties changepublic java.beans.PropertyChangeListener[] getPropertyChangeListeners()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |