Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Models and Support Classes Used by the JTable Class

The previous section described how to create tables from Java data types. These data types may not be the best representation for the data contained in your application. To allow data stored in any representation, the TableModel interface is defined. Classes containing data that you want to display in a table can implement this interface and then be used as the data model for one or more tables. The order of the columns in the table need not match the order of the columns in the data model. Moreover, the table need not display all the columns in the data model. To enable this type of functionality, the table model interface and table support classes must be understood. These are contained in the javax.swing.table package.

The TableModel Interface and Support Classes

The TableModel interface, shown in Listing 12.2, defines the methods that a class must implement to be used as the data source for a table. The methods getRowCount and getColumnCount define the size of the data structure. The data querying and setting methods, getValueAt and setValueAt, take integer parameters for the row and column of the data. These methods return and take an Object parameter, allowing any class of data to be stored in the data model. Moreover, each column in the model can contain a different class of Object. The getColumnName method returns the String name for the column. This is used in the table header, if displayed.

The getColumnClass method returns the class of object stored in that column of data. The JTable class uses this information to select appropriate editors and renderers for the table. The model must return true from the isCellEditable method for editing to be allowed. Finally, the class that implements the TableModel interface must manage TableModelListeners. As with all JFC models, it is the model’s responsibility to fire the appropriate event if the data in the model changes. For the TableModel, this is a TableModelEvent.

Listing 12.2 The TableModel Interface

public interface TableModel
{
    /**
     * Returns the number of records managed by the data source object. A
     * <B>JTable</B> uses this method to determine how many rows it
     * should create and display.  This method should be quick, as it
     * is called by <B>JTable</B> quite frequently.
     *
     * @return the number or rows in the model
     * @see #getColumnCount()
     */
    public int getRowCount();

    /**
     * Returns the number of columns managed by the data source object. A
     * <B>JTable</B> uses this method to determine how many columns it
     * should create and display on initialization.
     *
     * @return the number or columns in the model
     * @see #getRowCount()
     */
    public int getColumnCount();

    /**
     * Returns the name of the column at <i>columnIndex</i>.  This is
     * used to initialize the table’s column header name.  Note, this
     * name does not need to be unique.  Two columns on a table can have
     * the same name.
     *
     * @param columnIndex the index of column
     * @return  the name of the column
     */
    public String getColumnName(int columnIndex);

    /**
     * Returns the lowest common denominator Class in the column.  This
     * is used by the table to set up a default renderer and editor for
     * the column.
     *
     * @return the common ancestor class of the object values in
     *         the model.
     */
    public Class getColumnClass(int columnIndex);

    /**
     * Returns true if the cell at <I>rowIndex</I> and <I>columnIndex</I>

     * is editable.  Otherwise, setValueAt() on the cell will not change
     * the value of that cell.
     *
     * @param rowIndex the row whose value is to be looked up
     * @param columnIndex the column whose value is to be looked up
     * @return true if the cell is editable.
     * @see #setValueAt()
     */
    public boolean isCellEditable(int rowIndex, int columnIndex);

    /**
     * Returns an attribute value for the cell at <I>columnIndex</I>
     * and <I>rowIndex</I>.
     *
     * @param rowIndex the row whose value is to be looked up
     * @param columnIndex the column whose value is to be looked up
     * @return the value Object at the specified cell
     */
    public Object getValueAt(int rowIndex, int columnIndex);

    /**
     * Sets an attribute value for the record in the cell at
     * <I>columnIndex</I> and <I>rowIndex</I>.  <I>aValue</I> is
     * the new value.
     *
     * @param aValue the new value
     * @param rowIndex the row whose value is to be changed
     * @param columnIndex the column whose value is to be changed
     * @see #getValueAt()
     * @see #isCellEditable()
     */
    public void setValueAt(Object aValue, int rowIndex, int columnIndex);

    /**
     * Add a listener to the list that’s notified each time a change
     * to the data model occurs.
     *
     * @param l the TableModelListener
     */
    public void addTableModelListener(TableModelListener l);

    /**
     * Remove a listener from the list that’s notified each time a
     * change to the data model occurs.
     *
     * @param l the TableModelListener
     */
    public void removeTableModelListener(TableModelListener l);
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.