Groovy Documentation

com.wordpress.tipsforjava.swing.table
[Java] Class RowTableModel

java.lang.Object
  javax.swing.table.AbstractTableModel
      com.wordpress.tipsforjava.swing.table.RowTableModel

abstract class RowTableModel
extends AbstractTableModel

A TableModel that better supports the processing of rows of data. That is, the data is treated more like a row than an individual cell. Hopefully this class can be used as a parent class instead of extending the AbstractTableModel when you need custom models that contain row related data. A few methods have also been added to make it easier to customize properties of the model, such as the column class and column editability. Any class that extends this class must make sure to invoke the setRowClass() and setDataAndColumnNames() methods either indirectly, by using the various constructors, or indirectly.

Authors:
Rob Camick
Darryl Burke


Field Summary
protected Class[] columnClasses

protected List columnNames

protected Boolean[] isColumnEditable

private boolean isModelEditable

protected List modelData

private Class rowClass

 
Constructor Summary
protected RowTableModel(Class rowClass)

Constructs a RowTableModel with the row class.

protected RowTableModel(List columnNames)

Constructs a RowTableModel with column names.

protected RowTableModel(List modelData, List columnNames)

Constructs a RowTableModel with initial data and customized column names.

protected RowTableModel(List modelData, List columnNames, Class rowClass)

Full Constructor for creating a RowTableModel.

 
Method Summary
void addRow(Object rowData)

Adds a row of data to the end of the model.

static String formatColumnName(String columnName)

Class getColumnClass(int column)

Returns the Class of the queried column.

int getColumnCount()

Returns the number of columns in this table model.

String getColumnName(int column)

Returns the column name.

Object getRow(int row)

Returns the Object of the requested row.

int getRowCount()

Returns the number of rows in this table model.

Object[] getRowsAsArray(int... rows)

Returns an array of Objects for the requested rows.

List getRowsAsList(int... rows)

Returns a List of Objects for the requested rows.

void insertRow(int row, Object rowData)

Insert a row of data at the row location in the model.

void insertRows(int row, List rowList)

Insert multiple rows of data at the row location in the model.

void insertRows(int row, Object[] rowArray)

Insert multiple rows of data at the row location in the model.

boolean isCellEditable(int row, int column)

Returns true regardless of parameter values.

void moveRow(int start, int end, int to)

Moves one or more rows from the inlcusive range start to end to the to position in the model.

void removeRowRange(int start, int end)

Remove the specified rows from the model.

void removeRows(int... rows)

Remove the specified rows from the model.

void replaceRow(int row, Object rowData)

Replace a row of data at the row location in the model.

void setColumnClass(int column, Class columnClass)

Sets the Class for the specified column.

void setColumnEditable(int column, boolean isEditable)

Sets the editability for the specified column.

protected void setDataAndColumnNames(List modelData, List columnNames)

Reset the data and column names of the model.

void setModelEditable(boolean isModelEditable)

Set the ability to edit cell data for the entire table

protected void setRowClass(Class rowClass)

The class of the Row being stored in the TableModel

 
Methods inherited from class AbstractTableModel
getListeners, getColumnName, getColumnClass, isCellEditable, setValueAt, addTableModelListener, removeTableModelListener, fireTableStructureChanged, fireTableChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableCellUpdated, findColumn, getTableModelListeners, fireTableDataChanged, wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll, getRowCount, getColumnCount, getValueAt
 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

columnClasses

protected Class[] columnClasses


columnNames

protected List columnNames


isColumnEditable

protected Boolean[] isColumnEditable


isModelEditable

private boolean isModelEditable


modelData

protected List modelData


rowClass

private Class rowClass


 
Constructor Detail

RowTableModel

protected RowTableModel(Class rowClass)
Constructs a RowTableModel with the row class. This value is used by the getRowsAsArray() method. Sub classes creating a model using this constructor must make sure to invoke the setDataAndColumnNames() method.
Parameters:
rowClass - the class of row data to be added to the model


RowTableModel

protected RowTableModel(List columnNames)
Constructs a RowTableModel with column names. Each column's name will be taken from the columnNames List and the number of colums is determined by thenumber of items in the columnNames List. Sub classes creating a model using this constructor must make sure to invoke the setRowClass() method.
Parameters:
columnNames - List containing the names of the new columns


RowTableModel

protected RowTableModel(List modelData, List columnNames)
Constructs a RowTableModel with initial data and customized column names. Each item in the modelData List must also be a List Object containing items for each column of the row. Each column's name will be taken from the columnNames List and the number of colums is determined by thenumber of items in the columnNames List. Sub classes creating a model using this constructor must make sure to invoke the setRowClass() method.
Parameters:
modelData - the data of the table
columnNames - List containing the names of the new columns


RowTableModel

protected RowTableModel(List modelData, List columnNames, Class rowClass)
Full Constructor for creating a RowTableModel. Each item in the modelData List must also be a List Object containing items for each column of the row. Each column's name will be taken from the columnNames List and the number of colums is determined by thenumber of items in the columnNames List.
Parameters:
modelData the - data of the table
columnNames List - containing the names of the new columns
rowClass - the class of row data to be added to the model


 
Method Detail

addRow

public void addRow(Object rowData)
Adds a row of data to the end of the model. Notification of the row being added will be generated.
Parameters:
rowData - data of the row being added


formatColumnName

public static String formatColumnName(String columnName)


getColumnClass

public Class getColumnClass(int column)
Returns the Class of the queried column. First it will check to see if a Class has been specified for the column by using the setColumnClass method. If not, then the superclass value is returned.
Parameters:
column - the column being queried
Returns:
the Class of the column being queried


getColumnCount

public int getColumnCount()
Returns the number of columns in this table model.
Returns:
the number of columns in the model


getColumnName

public String getColumnName(int column)
Returns the column name.
Returns:
a name for this column using the string value of the appropriate member in columnNames. If columnNames does not have an entry for this index then the default name provided by the superclass is returned


getRow

public Object getRow(int row)
Returns the Object of the requested row.
Returns:
the Object of the requested row.


getRowCount

public int getRowCount()
Returns the number of rows in this table model.
Returns:
the number of rows in the model


getRowsAsArray

ssWarnings("unchecked")
	public
public Object[] getRowsAsArray(int... rows)
Returns an array of Objects for the requested rows.
Returns:
an array of Objects for the requested rows.


getRowsAsList

public List getRowsAsList(int... rows)
Returns a List of Objects for the requested rows.
Returns:
a List of Objects for the requested rows.


insertRow

public void insertRow(int row, Object rowData)
Insert a row of data at the row location in the model. Notification of the row being added will be generated.
Parameters:
row - row in the model where the data will be inserted
rowData - data of the row being added


insertRows

public void insertRows(int row, List rowList)
Insert multiple rows of data at the row location in the model. Notification of the row being added will be generated.
Parameters:
row - row in the model where the data will be inserted
rowList - each item in the list is a separate row of data


insertRows

public void insertRows(int row, Object[] rowArray)
Insert multiple rows of data at the row location in the model. Notification of the row being added will be generated.
Parameters:
row - row in the model where the data will be inserted
rowArray - each item in the Array is a separate row of data


isCellEditable

public boolean isCellEditable(int row, int column)
Returns true regardless of parameter values.
Parameters:
row - the row whose value is to be queried
column the - column whose value is to be queried
Returns:
true


moveRow

public void moveRow(int start, int end, int to)
Moves one or more rows from the inlcusive range start to end to the to position in the model. After the move, the row that was at index start will be at index to. This method will send a tableRowsUpdated notification message to all the listeners.

  Examples of moves:
  

1. moveRow(1,3,5); a|B|C|D|e|f|g|h|i|j|k - before a|e|f|g|h|B|C|D|i|j|k - after

2. moveRow(6,7,1); a|b|c|d|e|f|G|H|i|j|k - before a|G|H|b|c|d|e|f|i|j|k - after

Throws:
IllegalArgumentException if any of the elements would be moved out of the table's range
Parameters:
start - the starting row index to be moved
end - the ending row index to be moved
to - the destination of the rows to be moved


removeRowRange

public void removeRowRange(int start, int end)
Remove the specified rows from the model. Rows between the starting and ending indexes, inclusively, will be removed. Notification of the rows being removed will be generated.
Throws:
ArrayIndexOutOfBoundsException if any row index is invalid
Parameters:
start - starting row index
end - ending row index


removeRows

public void removeRows(int... rows)
Remove the specified rows from the model. The row indexes in the array must be in increasing order. Notification of the rows being removed will be generated.
Throws:
ArrayIndexOutOfBoundsException if any row index is invalid
Parameters:
rows - array containing indexes of rows to be removed


replaceRow

public void replaceRow(int row, Object rowData)
Replace a row of data at the row location in the model. Notification of the row being replaced will be generated.
Throws:
IllegalArgumentException when the Class of the row data does not match the row Class of the model.
Parameters:
row - row in the model where the data will be replaced
rowData - data of the row to replace the existing data


setColumnClass

public void setColumnClass(int column, Class columnClass)
Sets the Class for the specified column.
Throws:
ArrayIndexOutOfBoundsException if an invalid column was given
Parameters:
column - the column whose Class is being changed
columnClass - the new Class of the column


setColumnEditable

public void setColumnEditable(int column, boolean isEditable)
Sets the editability for the specified column.
Throws:
ArrayIndexOutOfBoundsException if an invalid column was given
Parameters:
column - the column whose Class is being changed
isEditable - indicates if the column is editable or not


setDataAndColumnNames

protected void setDataAndColumnNames(List modelData, List columnNames)
Reset the data and column names of the model. A fireTableStructureChanged event will be generated.
Parameters:
modelData - the data of the table
columnNames - List containing the names of the new columns


setModelEditable

public void setModelEditable(boolean isModelEditable)
Set the ability to edit cell data for the entire table Note: values set by the setColumnEditable(...) method will have prioritiy over this value.
Parameters:
isModelEditable - true/false


setRowClass

protected void setRowClass(Class rowClass)
The class of the Row being stored in the TableModel This is required for the getRowsAsArray() method to return the proper class of row.
Parameters:
rowClas - the class of the row


 

Groovy Documentation