com.cleancode.swing
Class SimpleTable

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.cleancode.swing.SimpleTable
All Implemented Interfaces:
Serializable, TableModel

public class SimpleTable
extends AbstractTableModel

Provides a table model based on AbstractTableModel, typically used with a JTable component. This class provides support for updating individual cells or the entire table. It also includes some diagnostic facilities; one may dump the entire table on command, or on events firing. This class defaults to all cells editable, but providing a JTableCellChecker allows selectivity over which cells are editable.

Since:
CleanCode 0.9
Version:
$Revision: 9 $
Author:
Michael Sorens
See Also:
JTable, JTableMgr, How to Use Tables, Serialized Form

Field Summary
static String VERSION
          Current version of this class.
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
SimpleTable(String diagName)
          Creates a new SimpleTable with a single column; all cells are editable.
SimpleTable(String diagName, String[] columnNames)
          Creates a new SimpleTable with all cells editable.
SimpleTable(String diagName, String[] columnNames, boolean editable)
          Creates a new SimpleTable with all cells editable or all cells immutable.
SimpleTable(String diagName, String[] columnNames, JTableCellChecker target)
          Creates a new SimpleTable with a custom cell checker.
 
Method Summary
 void addRow()
          Adds an empty row to the current table.
 void addRow(Object[] row)
          Adds the specified row to the current table.
 void copyTable(SimpleTable table)
          Copies the specified table into the current table.
 void copyTable(SimpleTable table, int startCol, int colCount)
          Copies the specified table into the current table, specifying the starting column number and the number of columns to copy.
 void deleteRow(int rowIndex)
          Deletes the specified row from the current table.
 void emptyTable()
          Empties the current table, resetting the number of rows to zero.
 void fillTable(Object[][] newData)
          Fills the table with new data (discarding any previously assigned).
 Class<?> getColumnClass(int colIndex)
          Method used by JTable to determine the default renderer/editor for each cell.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int colIndex)
          Returns the name of the specified column.
 int getRowCount()
          Returns the number of rows in the model.
 Object getValueAt(int rowIndex, int colIndex)
          Returns the value for the cell at the specified row and column indices.
 boolean isCellEditable(int rowIndex, int colIndex)
          JTable uses this method to determine whether a cell may be edited.
 void setValueAt(Object value, int rowIndex, int colIndex)
          Sets the value for the cell at the specified row and column indices.
 String toCondensedString()
          Returns a short string representation of this table.
 String toString()
          Returns a string representation of this table.
 
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, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Current version of this class.

Constructor Detail

SimpleTable

public SimpleTable(String diagName,
                   String[] columnNames,
                   JTableCellChecker target)
Creates a new SimpleTable with a custom cell checker.

Parameters:
diagName - display name of this table for diagnostic purposes
columnNames - display names of columns
target - cell checker object which determines cell editability

SimpleTable

public SimpleTable(String diagName,
                   String[] columnNames,
                   boolean editable)
Creates a new SimpleTable with all cells editable or all cells immutable.

Parameters:
diagName - display name of this table for diagnostic purposes
columnNames - display names of columns
editable - boolean indicating editable or not

SimpleTable

public SimpleTable(String diagName,
                   String[] columnNames)
Creates a new SimpleTable with all cells editable.

Parameters:
diagName - display name of this table for diagnostic purposes
columnNames - display names of columns

SimpleTable

public SimpleTable(String diagName)
Creates a new SimpleTable with a single column; all cells are editable.

Parameters:
diagName - display name of this table for diagnostic purposes
Method Detail

toString

public String toString()
Returns a string representation of this table. The string representation consists of the elements of each row of the table separated by tabs, and rows separated by the system-specific line separator.

Overrides:
toString in class Object
Returns:
a String representation of this table

toCondensedString

public String toCondensedString()
Returns a short string representation of this table. The string representation consists of the diagName, the number of rows, and the number of columns. These data are separated by commas and enclosed in braces.

Returns:
a String representation of this table

fillTable

public void fillTable(Object[][] newData)
Fills the table with new data (discarding any previously assigned).

Parameters:
newData - two-dimensional list of table data

copyTable

public void copyTable(SimpleTable table)
Copies the specified table into the current table. The specified table must have at least as many columns as the current table.

Parameters:
table - table to copy

copyTable

public void copyTable(SimpleTable table,
                      int startCol,
                      int colCount)
Copies the specified table into the current table, specifying the starting column number and the number of columns to copy.

Parameters:
table - table to copy
startCol - starting column number
colCount - number of columns to copy

emptyTable

public void emptyTable()
Empties the current table, resetting the number of rows to zero.


addRow

public void addRow(Object[] row)
Adds the specified row to the current table.

Parameters:
row - list of items comprising one row of the table

addRow

public void addRow()
Adds an empty row to the current table.


deleteRow

public void deleteRow(int rowIndex)
Deletes the specified row from the current table.

Parameters:
rowIndex - index of the row to delete

getColumnCount

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

Returns:
the number of columns in the model
See Also:
getRowCount()

getRowCount

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

Returns:
the number of rows in the model
See Also:
getColumnCount()

getColumnName

public String getColumnName(int colIndex)
Returns the name of the specified column. This is used to initialize the table's column header name.

Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel
Parameters:
colIndex - the index of the column
Returns:
the name of the column

getColumnClass

public Class<?> getColumnClass(int colIndex)
Method used by JTable to determine the default renderer/editor for each cell.

Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel
Parameters:
colIndex - the column whose value is to be queried
Returns:
class of the specified column

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int colIndex)
JTable uses this method to determine whether a cell may be edited.

Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel
Parameters:
rowIndex - row index of the cell
colIndex - column index of the cell
Returns:
boolean indicating whether cell is editable

getValueAt

public Object getValueAt(int rowIndex,
                         int colIndex)
Returns the value for the cell at the specified row and column indices.

Parameters:
rowIndex - row index of the cell
colIndex - column index of the cell
Returns:
value of the specified cell
See Also:
setValueAt(java.lang.Object, int, int)

setValueAt

public void setValueAt(Object value,
                       int rowIndex,
                       int colIndex)
Sets the value for the cell at the specified row and column indices.

Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel
Parameters:
value - new value for the cell
rowIndex - row index of the cell
colIndex - column index of the cell
See Also:
getValueAt(int, int)


CleanCode Java Libraries Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads