Chapter 29. The javax.swing.table Package
The javax.swing.table
package contains classes and interfaces that are used with the
powerful JTable component of the
javax.swing package. TableModel
is the central interface in this package; it provides access to the
data to be displayed in the table. TableColumn
represents the attributes of a column in the table.
TableColumnModel represents a set of columns in the
table. JTableHeader is a component that displays
the resizable and draggable column headers of a table. The
TableCellRenderer interface defines how individual
cells are drawn using a GUI component as a template.
Figure 29-1 shows the class hierarchy of
this package.
See Chapter 3, "Swing
Programming Topics", for a discussion of and example using
JTable and the javax.swing.table
package.
Figure 29-1. The javax.swing.table package
AbstractTableModel | Java 1.2 |
|
javax.swing.table | serializable model |
This abstract class is a convenient partial implementation of the
TableModel interface. An application that needs a
noneditable TableModel needs only to provide
implementations for the abstract methods
getColumnCount(), getRowCount(),
and getValueAt(). An application that wants to allow
the table data to be edited must additionally override the default
implementations of isCellEditable() and
setValueAt().
AbstractTableModel provides default implementations
of the event listener registration methods. Additionally, it defines
a number of methods for sending various types of
TableModelEvent objects to the registered
TableModelListener objects. This is useful because
of the fairly complex way that TableModelEvent is
used to describe different types of changes to the table data. If the
data underlying the table model ever changes, the application can call
one of these methods to notify the JTable, and any
other interested listeners, of the change.
public abstract class AbstractTableModel implements Serializable, TableModel { |
// | Public Constructors |
| public AbstractTableModel (); | |
// | Event Registration Methods (by event name) |
| public void addTableModelListener (javax.swing.event.TableModelListener l); | Implements:TableModel |
| public void removeTableModelListener (javax.swing.event.TableModelListener l); | Implements:TableModel |
// | Public Instance Methods |
| public int findColumn (String columnName); | |
| public void fireTableCellUpdated (int row, int column); | |
| public void fireTableChanged (javax.swing.event.TableModelEvent e); | |
| public void fireTableDataChanged (); | |
| public void fireTableRowsDeleted (int firstRow, int lastRow); | |
| public void fireTableRowsInserted (int firstRow, int lastRow); | |
| public void fireTableRowsUpdated (int firstRow, int lastRow); | |
| public void fireTableStructureChanged (); | |
// | Methods Implementing TableModel |
| public void addTableModelListener (javax.swing.event.TableModelListener l); | |
| public Class getColumnClass (int columnIndex); | |
| public abstract int getColumnCount (); | |
| public String getColumnName (int column); | |
| public abstract int getRowCount (); | |
| public abstract Object getValueAt (int rowIndex, int columnIndex); | |
| public boolean isCellEditable (int rowIndex, int columnIndex); | constant |
| public void removeTableModelListener (javax.swing.event.TableModelListener l); | |
| public void setValueAt (Object aValue, int rowIndex, int columnIndex); | empty |
// | Protected Instance Fields |
| protected javax.swing.event.EventListenerList listenerList ; | |
} |
Hierarchy: Object-->AbstractTableModel(Serializable,TableModel)
Subclasses: DefaultTableModel
DefaultTableCellRenderer | Java 1.2 |
|
javax.swing.table | serializable accessible swing component |
This class is a simple implementation of the
TableCellRenderer interface. It uses a
JLabel to display the textual value (determined by
calling the toString() method) of any object.
DefaultTableCellRenderer takes care to use the colors
and fonts of the JTable, so that it interfaces
seamlessly with the table.
public class DefaultTableCellRenderer extends JLabel implements Serializable, TableCellRenderer { |
// | Public Constructors |
| public DefaultTableCellRenderer (); | |
// | Inner Classes |
| ; | |
// | Methods Implementing TableCellRenderer |
| public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column); | |
// | Public Methods Overriding JLabel |
| public void updateUI (); | |
// | Public Methods Overriding JComponent |
| public void setBackground (java.awt.Color c); | |
| public void setForeground (java.awt.Color c); | |
// | Protected Instance Methods |
| protected void setValue (Object value); | |
// | Protected Class Fields |
| protected static javax.swing.border.Border noFocusBorder ; | |
} |
Hierarchy: Object-->Component(java.awt.image.ImageObserver,java.awt.MenuContainer,Serializable)-->Container-->JComponent(Serializable)-->JLabel(Accessible,SwingConstants)-->DefaultTableCellRenderer(Serializable,TableCellRenderer)
Subclasses: DefaultTableCellRenderer.UIResource
DefaultTableCellRenderer.UIResource | Java 1.2 |
|
javax.swing.table | serializable accessible swing component |
This class is a trivial subclass of
DefaultTableCellRenderer that exists only to
implement the javax.swing.plaf.UIResource marker
interface.
public static class DefaultTableCellRenderer.UIResource extends DefaultTableCellRenderer implements javax.swing.plaf.UIResource { |
// | Public Constructors |
| public UIResource (); | |
} |
DefaultTableColumnModel | Java 1.2 |
|
javax.swing.table | serializable model |
This class implements the TableColumnModel and
keeps track of the number, order, size, and selection state of the
columns in a JTable component. The
JTable uses this class by default; few applications
have any reason to use anything else.
public class DefaultTableColumnModel implements javax.swing.event.ListSelectionListener, java.beans.PropertyChangeListener, Serializable, TableColumnModel { |
// | Public Constructors |
| public DefaultTableColumnModel (); | |
// | Methods Implementing ListSelectionListener |
| public void valueChanged (javax.swing.event.ListSelectionEvent e); | |
// | Methods Implementing PropertyChangeListener |
| public void propertyChange (java.beans.PropertyChangeEvent evt); | |
// | Methods Implementing TableColumnModel |
| public void addColumn (TableColumn aColumn); | |
| public void addColumnModelListener (javax.swing.event.TableColumnModelListener x); | |
| public TableColumn getColumn (int columnIndex); | |
| public int getColumnCount (); | default:0 |
| public int getColumnIndex (Object identifier); | |
| public int getColumnIndexAtX (int xPosition); | |
| public int getColumnMargin (); | default:1 |
| public java.util.Enumeration getColumns (); | |
| public boolean getColumnSelectionAllowed (); | default:false |
| public int getSelectedColumnCount (); | default:0 |
| public int[ ] getSelectedColumns (); | |
| public ListSelectionModel getSelectionModel (); | default:DefaultListSelectionModel |
| public int getTotalColumnWidth (); | default:0 |
| public void moveColumn (int columnIndex, int newIndex); | |
| public void removeColumn (TableColumn column); | |
| public void removeColumnModelListener (javax.swing.event.TableColumnModelListener x); | |
| public void setColumnMargin (int newMargin); | |
| public void setColumnSelectionAllowed (boolean flag); | |
| public void setSelectionModel (ListSelectionModel newModel); | |
// | Protected Instance Methods |
| protected ListSelectionModel createSelectionModel (); | |
| protected void fireColumnAdded (javax.swing.event.TableColumnModelEvent e); | |
| protected void fireColumnMarginChanged (); | |
| protected void fireColumnMoved (javax.swing.event.TableColumnModelEvent e); | |
| protected void fireColumnRemoved (javax.swing.event.TableColumnModelEvent e); | |
| protected void fireColumnSelectionChanged (javax.swing.event.ListSelectionEvent e); | |
| protected void recalcWidthCache (); | |
// | Protected Instance Fields |
| protected transient javax.swing.event.ChangeEvent changeEvent ; | |
| protected int columnMargin ; | |
| protected boolean columnSelectionAllowed ; | |
| protected javax.swing.event.EventListenerList listenerList ; | |
| protected ListSelectionModel selectionModel ; | |
| protected java.util.Vector tableColumns ; | |
| protected int totalColumnWidth ; | |
} |
Hierarchy: Object-->DefaultTableColumnModel(javax.swing.event.ListSelectionListener(java.util.EventListener),java.beans.PropertyChangeListener(java.util.EventListener),Serializable,TableColumnModel)
DefaultTableModel | Java 1.2 |
|
javax.swing.table | serializable model |
This class is a relatively simple implementation of
TableModel that works with table data that is
expressed either as
an array of rows, where each row is an array of objects, or as a
vector of rows, where each row is a vector of objects. In addition to
the table cell data, DefaultTableModel also allows
you to specify the column header values in an array or vector.
DefaultTableModel is the only concrete
TableModel implementation in Swing. Several of the
JTable constructors initialize and use a
DefaultTableModel object as the component's model.
In addition to its TableModel methods,
DefaultTableModel also defines methods for changing
the table data, adding and removing columns, and so on.
public class DefaultTableModel extends AbstractTableModel implements Serializable { |
// | Public Constructors |
| public DefaultTableModel (); | |
| public DefaultTableModel (Object[ ] columnNames, int numRows); | |
| public DefaultTableModel (java.util.Vector data, java.util.Vector columnNames); | |
| public DefaultTableModel (Object[ ][ ] data, Object[ ] columnNames); | |
| public DefaultTableModel (int numRows, int numColumns); | |
| public DefaultTableModel (java.util.Vector columnNames, int numRows); | |
// | Protected Class Methods |
| protected static java.util.Vector convertToVector (Object[ ][ ] anArray); | |
| protected static java.util.Vector convertToVector (Object[ ] anArray); | |
// | Property Accessor Methods (by property name) |
| public int getColumnCount (); | Overrides:AbstractTableModel default:0 |
| public java.util.Vector getDataVector (); | |
| public int getRowCount (); | Overrides:AbstractTableModel default:0 |
// | Public Instance Methods |
| public void addColumn (Object columnName); | |
| public void addColumn (Object columnName, java.util.Vector columnData); | |
| public void addColumn (Object columnName, Object[ ] columnData); | |
| public void addRow (Object[ ] rowData); | |
| public void addRow (java.util.Vector rowData); | |
| public void insertRow (int row, Object[ ] rowData); | |
| public void insertRow (int row, java.util.Vector rowData); | |
| public void moveRow (int startIndex, int endIndex, int toIndex); | |
| public void newDataAvailable (javax.swing.event.TableModelEvent event); | |
| public void newRowsAdded (javax.swing.event.TableModelEvent event); | |
| public void removeRow (int row); | |
| public void rowsRemoved (javax.swing.event.TableModelEvent event); | |
| public void setColumnIdentifiers (Object[ ] newIdentifiers); | |
| public void setColumnIdentifiers (java.util.Vector newIdentifiers); | |
| public void setDataVector (Object[ ][ ] newData, Object[ ] columnNames); | |
| public void setDataVector (java.util.Vector newData, java.util.Vector columnNames); | |
| public void setNumRows (int newSize); | |
// | Public Methods Overriding AbstractTableModel |
| public String getColumnName (int column); | |
| public Object getValueAt (int row, int column); | |
| public boolean isCellEditable (int row, int column); | constant |
| public void setValueAt (Object aValue, int row, int column); | |
// | Protected Instance Fields |
| protected java.util.Vector columnIdentifiers ; | |
| protected java.util.Vector dataVector ; | |
} |
Hierarchy: Object-->AbstractTableModel(Serializable,TableModel)-->DefaultTableModel(Serializable)
JTableHeader | Java 1.2 |
|
javax.swing.table | serializable accessible swing component |
This class is a Swing component that displays the header of a
JTable. This header component displays the name of
each column and, optionally, allows the user to resize and
reorder the columns by dragging them. JTableHeader
uses the TableColumnModel of its
JTable to obtain
information about the column headers it must display.
A JTable component automatically creates a
suitable JTableHeader component; an application
should not have to create one of its own. Nevertheless,
JTableHeader does define some interesting methods
that applications may want to use. Obtain the
JTableHeader of a JTable with the
getTableHeader() method of
JTable. Once you have the
JTableHeader object, use its
resizingAllowed and
reorderingAllowed properties to specify how the
user is allowed to manipulate the columns. Also, set the
updateTableInRealTime property to specify if
the entire JTable should be updated as the user
drags a column or if the update should be postponed until the
user completes the drag.
public class JTableHeader extends JComponent implements Accessible, javax.swing.event.TableColumnModelListener { |
// | Public Constructors |
| public JTableHeader (); | |
| public JTableHeader (TableColumnModel cm); | |
// | Inner Classes |
| ; | |
// | Property Accessor Methods (by property name) |
| public AccessibleContext getAccessibleContext (); | Implements:Accessible default:AccessibleJTableHeader |
| public TableColumnModel getColumnModel (); | default:DefaultTableColumnModel |
| public void setColumnModel (TableColumnModel newModel); | |
| public TableColumn getDraggedColumn (); | default:null |
| public void setDraggedColumn (TableColumn aColumn); | |
| public int getDraggedDistance (); | default:0 |
| public void setDraggedDistance (int distance); | |
| public boolean getReorderingAllowed (); | default:true |
| public void setReorderingAllowed (boolean b); | |
| public boolean getResizingAllowed (); | default:true |
| public void setResizingAllowed (boolean b); | |
| public TableColumn getResizingColumn (); | default:null |
| public void setResizingColumn (TableColumn aColumn); | |
| public JTable getTable (); | default:null |
| public void setTable (JTable aTable); | |
| public javax.swing.plaf.TableHeaderUI getUI (); | |
| public void setUI (javax.swing.plaf.TableHeaderUI ui); | |
| public String getUIClassID (); | Overrides:JComponent default:"TableHeaderUI" |
| public boolean getUpdateTableInRealTime (); | default:true |
| public void setUpdateTableInRealTime (boolean flag); | |
// | Public Instance Methods |
| public int columnAtPoint (java.awt.Point point); | |
| public java.awt.Rectangle getHeaderRect (int columnIndex); | |
| public void resizeAndRepaint (); | |
// | Methods Implementing Accessible |
| public AccessibleContext getAccessibleContext (); | default:AccessibleJTableHeader |
// | Methods Implementing TableColumnModelListener |
| public void columnAdded (javax.swing.event.TableColumnModelEvent e); | |
| public void columnMarginChanged (javax.swing.event.ChangeEvent e); | |
| public void columnMoved (javax.swing.event.TableColumnModelEvent e); | |
| public void columnRemoved (javax.swing.event.TableColumnModelEvent e); | |
| public void columnSelectionChanged (javax.swing.event.ListSelectionEvent e); | empty |
// | Public Methods Overriding JComponent |
| public String getToolTipText (java.awt.event.MouseEvent event); | |
| public void updateUI (); | |
// | Protected Methods Overriding JComponent |
| protected String paramString (); | |
// | Protected Instance Methods |
| protected TableColumnModel createDefaultColumnModel (); | |
| protected void initializeLocalVars (); | |
// | Protected Instance Fields |
| protected TableColumnModel columnModel ; | |
| protected transient TableColumn draggedColumn ; | |
| protected transient int draggedDistance ; | |
| protected boolean reorderingAllowed ; | |
| protected boolean resizingAllowed ; | |
| protected transient TableColumn resizingColumn ; | |
| protected JTable table ; | |
| protected boolean updateTableInRealTime ; | |
} |
Hierarchy: Object-->Component(java.awt.image.ImageObserver,java.awt.MenuContainer,Serializable)-->Container-->JComponent(Serializable)-->JTableHeader(Accessible,javax.swing.event.TableColumnModelListener(java.util.EventListener))
Passed To: JTable.setTableHeader(), JTableHeader.AccessibleJTableHeader.AccessibleJTableHeaderEntry.AccessibleJTableHeaderEntry()
Returned By: JTable.{createDefaultTableHeader(), getTableHeader()}
Type Of: JTable.tableHeader
TableCellEditor | Java 1.2 |
|
javax.swing.table | |
This interface extends CellEditor and adds an
additional method that must be implemented by classes that want to
serve as editors for table cells displayed by a
JTable. Most applications can rely on the
default set of table-cell-editing capabilities of
javax.
swing.DefaultCellEditor and do not have to
implement this interface. If you do need to implement this
interface, see javax.swing.CellEditor and
javax.swing.tree.TreeCellEditor for further
details.
public abstract interface TableCellEditor extends CellEditor { |
// | Public Instance Methods |
| public abstract Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column); | |
} |
Hierarchy: (TableCellEditor(CellEditor))
Implementations: DefaultCellEditor
Passed To: JTable.{prepareEditor(), setCellEditor(), setDefaultEditor()}, TableColumn.{setCellEditor(), TableColumn()}
Returned By: JTable.{getCellEditor(), getDefaultEditor()}, TableColumn.getCellEditor()
Type Of: JTable.cellEditor, TableColumn.cellEditor
TableCellRenderer | Java 1.2 |
|
javax.swing.table | |
This interface defines the method that must be implemented by any
class wishing to display data within a JTable
component. getTableCellRendererComponent() is
passed the value that appears in a specified table cell; it must
return a Component object capable of displaying
that value in some fashion. Other arguments to the method specify
whether the cell is selected and whether it has the keyboard focus.
A renderer should take these factors into account when deciding how to
display the value. The JTable is
responsible for positioning the returned Component
properly and causing it to draw itself.
The TableCellRenderer simply has to configure the
appearance and content of the component before returning it.
Most applications can rely on the
DefaultTableCellRenderer class to display a textual
representation of any object and do not have to implement this
interface themselves.
public abstract interface TableCellRenderer { |
// | Public Instance Methods |
| public abstract Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column); | |
} |
Implementations: DefaultTableCellRenderer
Passed To: JTable.{prepareRenderer(), setDefaultRenderer()}, TableColumn.{setCellRenderer(), setHeaderRenderer(), TableColumn()}
Returned By: JTable.{getCellRenderer(), getDefaultRenderer()}, TableColumn.{createDefaultHeaderRenderer(), getCellRenderer(), getHeaderRenderer()}
Type Of: TableColumn.{cellRenderer, headerRenderer}
TableColumn | Java 1.2 |
|
javax.swing.table | serializable |
This class contains information about a single column displayed within
a JTable. The JTable component
creates TableColumn objects automatically, and
applications rarely need to create their own. To obtain the
TableColumn objects automatically created by a
JTable, first obtain the
TableColumnModel of the table and then use its
methods to query the individual columns.
TableColumn exposes a number of useful
properties. width,
preferredWidth, minWidth, and
maxWidth specify the current, preferred, minimum,
and maximum sizes for the column. The resizable
property specifies whether the user is allowed to resize the column.
The identifier property allows you to attach a name
to a column, which can sometimes be useful when working with columns
that may be reordered. The identifier is never
displayed. On the other hand, the headerValue
property specifies the object (usually a String)
that is displayed in the column header. The
headerValue is displayed by the
TableCellRenderer specified with the
headerRenderer property. Do not confuse the
headerRenderer with the
cellRenderer and cellEditor
properties. If these properties are not null, they
specify a custom renderer object and a custom editor object to be used
for the cells in this column.
Finally, the modelIndex property specifies the
column number to be used when extracting data for this column from the
TableModel. Since JTable allows
its columns to be rearranged by the user, there are two different
coordinate systems for referring to columns. The first is the index
of the TableColumn object within the
TableColumnModel. This is the visual order of
columns as displayed by the JTable. The other
coordinate system is the more fundamental index of the column data
within the underlying TableModel. The
modelIndex property
uses the latter coordinate system and specifies where to obtain
data for the column.
public class TableColumn implements Serializable { |
// | Public Constructors |
| public TableColumn (); | |
| public TableColumn (int modelIndex); | |
| public TableColumn (int modelIndex, int width); | |
| public TableColumn (int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor); | |
// | Public Constants |
| public static final String CELL_RENDERER_PROPERTY ; | ="cellRenderer" |
| public static final String COLUMN_WIDTH_PROPERTY ; | ="columWidth" |
| public static final String HEADER_RENDERER_PROPERTY ; | ="headerRenderer" |
| public static final String HEADER_VALUE_PROPERTY ; | ="headerValue" |
// | Event Registration Methods (by event name) |
| public void addPropertyChangeListener (java.beans.PropertyChangeListener listener); | synchronized |
| public void removePropertyChangeListener (java.beans.PropertyChangeListener listener); | synchronized |
// | Property Accessor Methods (by property name) |
| public TableCellEditor getCellEditor (); | default:null |
| public void setCellEditor (TableCellEditor anEditor); | |
| public TableCellRenderer getCellRenderer (); | default:null |
| public void setCellRenderer (TableCellRenderer aRenderer); | |
| public TableCellRenderer getHeaderRenderer (); | |
| public void setHeaderRenderer (TableCellRenderer aRenderer); | |
| public Object getHeaderValue (); | default:null |
| public void setHeaderValue (Object aValue); | |
| public Object getIdentifier (); | default:null |
| public void setIdentifier (Object anIdentifier); | |
| public int getMaxWidth (); | default:2147483647 |
| public void setMaxWidth (int maxWidth); | |
| public int getMinWidth (); | default:15 |
| public void setMinWidth (int minWidth); | |
| public int getModelIndex (); | default:0 |
| public void setModelIndex (int anIndex); | |
| public int getPreferredWidth (); | default:75 |
| public void setPreferredWidth (int preferredWidth); | |
| public boolean getResizable (); | default:true |
| public void setResizable (boolean flag); | |
| public int getWidth (); | default:75 |
| public void setWidth (int width); | |
// | Public Instance Methods |
| public void disableResizedPosting (); | |
| public void enableResizedPosting (); | |
| public void sizeWidthToFit (); | |
// | Protected Instance Methods |
| protected TableCellRenderer createDefaultHeaderRenderer (); | |
// | Protected Instance Fields |
| protected TableCellEditor cellEditor ; | |
| protected TableCellRenderer cellRenderer ; | |
| protected TableCellRenderer headerRenderer ; | |
| protected Object headerValue ; | |
| protected Object identifier ; | |
| protected boolean isResizable ; | |
| protected int maxWidth ; | |
| protected int minWidth ; | |
| protected int modelIndex ; | |
| protected transient int resizedPostingDisableCount ; | |
| protected int width ; | |
} |
Hierarchy: Object-->TableColumn(Serializable)
Passed To: JTable.{addColumn(), removeColumn()}, DefaultTableColumnModel.{addColumn(), removeColumn()}, JTableHeader.{setDraggedColumn(), setResizingColumn()}, TableColumnModel.{addColumn(), removeColumn()}
Returned By: JTable.getColumn(), DefaultTableColumnModel.getColumn(), JTableHeader.{getDraggedColumn(), getResizingColumn()}, TableColumnModel.getColumn()
Type Of: JTableHeader.{draggedColumn, resizingColumn}
TableColumnModel | Java 1.2 |
|
javax.swing.table | model |
A JTable component uses a
TableColumnModel object to keep track of the
columns it displays, the order they are in, and their
selection state. In essence, a TableColumnModel
maintains a list of TableColumn objects and
remembers which are selected. Most applications rely on the
DefaultTableColumnModel implementation and
rarely need to implement this interface themselves.
public abstract interface TableColumnModel { |
// | Property Accessor Methods (by property name) |
| public abstract int getColumnCount (); | |
| public abstract int getColumnMargin (); | |
| public abstract void setColumnMargin (int newMargin); | |
| public abstract java.util.Enumeration getColumns (); | |
| public abstract boolean getColumnSelectionAllowed (); | |
| public abstract void setColumnSelectionAllowed (boolean flag); | |
| public abstract int getSelectedColumnCount (); | |
| public abstract int[ ] getSelectedColumns (); | |
| public abstract ListSelectionModel getSelectionModel (); | |
| public abstract void setSelectionModel (ListSelectionModel newModel); | |
| public abstract int getTotalColumnWidth (); | |
// | Public Instance Methods |
| public abstract void addColumn (TableColumn aColumn); | |
| public abstract void addColumnModelListener (javax.swing.event.TableColumnModelListener x); | |
| public abstract TableColumn getColumn (int columnIndex); | |
| public abstract int getColumnIndex (Object columnIdentifier); | |
| public abstract int getColumnIndexAtX (int xPosition); | |
| public abstract void moveColumn (int columnIndex, int newIndex); | |
| public abstract void removeColumn (TableColumn column); | |
| public abstract void removeColumnModelListener (javax.swing.event.TableColumnModelListener x); | |
} |
Implementations: DefaultTableColumnModel
Passed To: JTable.{JTable(), setColumnModel()}, javax.swing.event.TableColumnModelEvent.TableColumnModelEvent(), JTableHeader.{JTableHeader(), setColumnModel()}
Returned By: JTable.{createDefaultColumnModel(), getColumnModel()}, JTableHeader.{createDefaultColumnModel(), getColumnModel()}
Type Of: JTable.columnModel, JTableHeader.columnModel
TableModel | Java 1.2 |
|
javax.swing.table | model |
This interface is the intermediary between a JTable
component and the data it displays. Every JTable
uses a TableModel to encapsulate its data.
getColumnCount() and
getRowCount() return the size of the table.
getColumnName() returns the header text for a given
column number. getColumnClass() returns the
Class object for a numbered column. (If you want to
display different types of objects in a column, this method should
return values of type Object.) The most
important method of the interface, however, is
getValueAt(), which, given a column number and a row
number, returns the cell value. The JTable class
can be configured to allow the user to reorder columns by dragging
them. Note that this visual reordering does not change the underlying
column numbers used to access data from the TableModel.
If you are allowing users to edit data in your
TableModel, you must also provide meaningful
implementations of isCellEditable() and
setValueAt(). If the model can be edited or if
the data it contains can otherwise change (e.g., if rows are
added), you must also implement
addTableModelListener() and
removeTableModelListener() and send a
TableModelEvent when the contents of the table
change.
Applications with simple table display needs can rely on
the DefaultTableModel. Because tabular data can
come from an wide variety of sources, in a wide variety of formats,
however, many applications need a custom
TableModel implementation.
Most applications
find it easer to subclass AbstractTableModel than
to implement TableModel from scratch.
public abstract interface TableModel { |
// | Event Registration Methods (by event name) |
| public abstract void addTableModelListener (javax.swing.event.TableModelListener l); | |
| public abstract void removeTableModelListener (javax.swing.event.TableModelListener l); | |
// | Public Instance Methods |
| public abstract Class getColumnClass (int columnIndex); | |
| public abstract int getColumnCount (); | |
| public abstract String getColumnName (int columnIndex); | |
| public abstract int getRowCount (); | |
| public abstract Object getValueAt (int rowIndex, int columnIndex); | |
| public abstract boolean isCellEditable (int rowIndex, int columnIndex); | |
| public abstract void setValueAt (Object aValue, int rowIndex, int columnIndex); | |
} |
Implementations: AbstractTableModel
Passed To: JTable.{JTable(), setModel()}, javax.swing.event.TableModelEvent.TableModelEvent()
Returned By: JTable.{createDefaultDataModel(), getModel()}
Type Of: JTable.dataModel
| | |
28.1. The javax.swing.plaf Package | | 30. The javax.swing.text Package |
Copyright © 2001 O'Reilly & Associates. All rights reserved.
|