net.luniks.swing
Class IntroTableModel

java.lang.Object
  extended byjavax.swing.table.AbstractTableModel
      extended bynet.luniks.swing.IntroTableModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel

public class IntroTableModel
extends javax.swing.table.AbstractTableModel

Table model that takes Java beans as rows. The properties of the beans correspond to the columns of the table, their respective values are rendered depending on their type and can be set by editing the cells. For each property there has to be a public getter and setter method, the field itself is optional.

Author:
Torsten Römer, www.luniks.net
See Also:
Serialized Form

Field Summary
private static java.lang.Object[] ARGS
           
static int ASCENDING
           
private  java.lang.String[] columnNames
           
static int DESCENDING
           
private  boolean[] editable
           
private static java.lang.String GET
           
private  java.lang.reflect.Method[] getters
           
private static java.lang.Class[] PARAM_TYPES
           
private  java.lang.String[] properties
           
private  java.lang.Object[] rows
           
private static java.lang.String SET
           
private  java.lang.reflect.Method[] setters
           
private  javax.swing.JTable table
           
static int UNCHANGED
           
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
IntroTableModel(java.lang.Object[] rows, java.lang.String[] properties, java.lang.String[] columnNames, boolean[] editable)
          Constructs the table model.
 
Method Summary
 int addRow(java.lang.Object object)
          Adds the object given as argument as last row to the table.
 void deleteRow(int row)
          Deletes the row at the index given as argument and calls fireTableRowsDeleted with the index of the deleted row as both arguments.
 java.lang.Class getColumnClass(int col)
          Returns the type of the column at the index given as argument.
 int getColumnCount()
          Returns the number of columns
 java.lang.String getColumnName(int col)
          Returns the name of the column with the index given as argument
 java.lang.Object getObjectAt(int row)
          Returns the bean at the row index given as argument
 int getRowCount()
          Returns the number of rows
 java.lang.Object[] getRows()
          Returns all rows/beans of the table
 java.lang.Object getValueAt(int row, int col)
          Returns the value at the row and column index given as first and second argument.
 boolean isCellEditable(int row, int col)
          Returns true if the column index given as second argument is editable (row is ignored), false otherwise.
 void moveRowToFirst(int row)
          Moves the row at the index given as argument to be the first row in the table.
 void moveRowToLast(int row)
          Moves the row at the index given as argument to be the last row in the table.
 void setObjectAt(java.lang.Object object, int row)
          Replaces the bean at the row index given as second argument with the bean given as first argument.
 void setRows(java.lang.Object[] rows)
          Replaces all rows/beans with the ones given as argument and calls fireTableDataChanged
 void setTable(javax.swing.JTable table)
          If the table this TableModel belongs to is given as argument, the table row selection is maintained when sorting using sort
 void setValueAt(java.lang.Object value, int row, int col)
          Sets the value of the bean property at the row and column index given as second and third argument to the value given as first argument.
 void sort(int col, int dir)
          Sorts the rows by the column index given as first argument in natural order in the direction given as second argument.
 void swapRows(int rowA, int rowB)
          Swaps the position of the two rows given as first and second argument.
 void update()
          This method simply calls fireTableDataChanged
 void update(int row)
          This method simply calls fireTableRowsUpdated with the row index given as argument as both arguments
 
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
 

Field Detail

ASCENDING

public static final int ASCENDING
See Also:
Constant Field Values

DESCENDING

public static final int DESCENDING
See Also:
Constant Field Values

UNCHANGED

public static final int UNCHANGED
See Also:
Constant Field Values

GET

private static final java.lang.String GET
See Also:
Constant Field Values

SET

private static final java.lang.String SET
See Also:
Constant Field Values

ARGS

private static final java.lang.Object[] ARGS

PARAM_TYPES

private static final java.lang.Class[] PARAM_TYPES

rows

private java.lang.Object[] rows

properties

private java.lang.String[] properties

columnNames

private java.lang.String[] columnNames

editable

private boolean[] editable

getters

private java.lang.reflect.Method[] getters

setters

private java.lang.reflect.Method[] setters

table

private javax.swing.JTable table
Constructor Detail

IntroTableModel

public IntroTableModel(java.lang.Object[] rows,
                       java.lang.String[] properties,
                       java.lang.String[] columnNames,
                       boolean[] editable)
                throws java.lang.NoSuchMethodException
Constructs the table model.

Parameters:
rows - the beans that should be rendered in a table using this model
properties - the names of the bean properties that should be rendered columnwise
columnNames - the column names, number and order must match the properties
editable - sets the columns to editable or not, number and order must match the properties and column names
Method Detail

setTable

public void setTable(javax.swing.JTable table)
If the table this TableModel belongs to is given as argument, the table row selection is maintained when sorting using sort


update

public void update()
This method simply calls fireTableDataChanged


update

public void update(int row)
This method simply calls fireTableRowsUpdated with the row index given as argument as both arguments

Parameters:
row - the index of the row that should be updated

setObjectAt

public void setObjectAt(java.lang.Object object,
                        int row)
Replaces the bean at the row index given as second argument with the bean given as first argument. Calls fireTableRowsUpdated with the row index as both arguments

Parameters:
object - the bean to replace the existing one
row - the row index at which to replace the bean

getObjectAt

public java.lang.Object getObjectAt(int row)
Returns the bean at the row index given as argument

Parameters:
row - the row index of the bean to be returned
Returns:
the bean at the specified index

setRows

public void setRows(java.lang.Object[] rows)
Replaces all rows/beans with the ones given as argument and calls fireTableDataChanged

Parameters:
rows - the rows to replace the existing ones

getRows

public java.lang.Object[] getRows()
Returns all rows/beans of the table

Returns:
all rows

getColumnCount

public int getColumnCount()
Returns the number of columns

Returns:
number of columns

getRowCount

public int getRowCount()
Returns the number of rows

Returns:
number of rows

getColumnName

public java.lang.String getColumnName(int col)
Returns the name of the column with the index given as argument

Parameters:
col - the column index
Returns:
the column name

getValueAt

public java.lang.Object getValueAt(int row,
                                   int col)
Returns the value at the row and column index given as first and second argument. Primitive types are returned as their corresponding wrapper class

Parameters:
row - the row index
col - the column index
Returns:
the value

getColumnClass

public java.lang.Class getColumnClass(int col)
Returns the type of the column at the index given as argument. This is actually the return type of the corresponding bean property's getter method. Primitive types are returned as their corresponding wrapper class, matching the object type returned by getValueAt for the same column.

Parameters:
col - the column index
Returns:
the type of the column

isCellEditable

public boolean isCellEditable(int row,
                              int col)
Returns true if the column index given as second argument is editable (row is ignored), false otherwise.

Parameters:
row - the row index (ignored)
col - the column index
Returns:
true if the cell is editable, false otherwise

setValueAt

public void setValueAt(java.lang.Object value,
                       int row,
                       int col)
Sets the value of the bean property at the row and column index given as second and third argument to the value given as first argument.

Parameters:
value - the value to be set. Calls fireTableCellUpdated with row and column index as first and second argument
row - the row index
col - the column index

swapRows

public void swapRows(int rowA,
                     int rowB)
Swaps the position of the two rows given as first and second argument. Does not notify any listeners. After calling this method the table has to be repainted to visually reflect the change.

Parameters:
rowA - the index of one of the two rows to be swapped
rowB - the index of the other of the two rows to be swapped

moveRowToFirst

public void moveRowToFirst(int row)
Moves the row at the index given as argument to be the first row in the table. Does not notify any listeners. After calling this method the table has to be repainted to visually reflect the change.

Parameters:
row - the index of the row to be moved to be the first row

moveRowToLast

public void moveRowToLast(int row)
Moves the row at the index given as argument to be the last row in the table. Does not notify any listeners. After calling this method the table has to be repainted to visually reflect the change.

Parameters:
row - the index of the row to be moved to be the last row

addRow

public int addRow(java.lang.Object object)
Adds the object given as argument as last row to the table. Calls fireTableRowsInserted with the index of the added row as both arguments.

Parameters:
object - the object to be added to the table
Returns:
the index of the added row

deleteRow

public void deleteRow(int row)
Deletes the row at the index given as argument and calls fireTableRowsDeleted with the index of the deleted row as both arguments. Does nothing if the index does not exist.

Parameters:
row - the index of the row to be deleted

sort

public void sort(int col,
                 int dir)
          throws java.lang.Exception
Sorts the rows by the column index given as first argument in natural order in the direction given as second argument. Values for directions are: ASCENDING, DESCENDING and UNCHANGED. Does nothing if the column type does not implement Comparable, or the column index is out of range. If the JTable to which this TableModel belongs to has been set using setTable, the row selection is maintained.

Throws:
java.lang.Exception


Copyright (c) 2004, 2005 Torsten Römer, dode@luniks.net