|
JavaTM 2 Platform Std. Ed. v1.4.2 |
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JTable
The JTable is used to display and edit regular two-dimensional tables
of cells.
See How to Use Tables
in The Java Tutorial
for task-oriented documentation and examples of using JTable.
The JTable has many
facilities that make it possible to customize its rendering and editing
but provides defaults for these features so that simple tables can be
set up easily. For example, to set up a table with 10 rows and 10
columns of numbers:
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
JTable table = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(table);
Note that if you wish to use a JTable in a standalone
view (outside of a JScrollPane) and want the header
displayed, you can get it using getTableHeader() and
display it separately.
When designing applications that use the JTable it is worth paying
close attention to the data structures that will represent the table's data.
The DefaultTableModel is a model implementation that
uses a Vector of Vectors of Objects to
store the cell values. As well as copying the data from an
application into the DefaultTableModel,
it is also possible to wrap the data in the methods of the
TableModel interface so that the data can be passed to the
JTable directly, as in the example above. This often results
in more efficient applications because the model is free to choose the
internal representation that best suits the data.
A good rule of thumb for deciding whether to use the AbstractTableModel
or the DefaultTableModel is to use the AbstractTableModel
as the base class for creating subclasses and the DefaultTableModel
when subclassing is not required.
The "TableExample" directory in the demo area of the source distribution
gives a number of complete examples of JTable usage,
covering how the JTable can be used to provide an
editable view of data taken from a database and how to modify
the columns in the display to use specialized renderers and editors.
The JTable uses integers exclusively to refer to both the rows and the columns
of the model that it displays. The JTable simply takes a tabular range of cells
and uses getValueAt(int, int) to retrieve the
values from the model during painting.
By default, columns may be rearranged in the JTable so that the
view's columns appear in a different order to the columns in the model.
This does not affect the implementation of the model at all: when the
columns are reordered, the JTable maintains the new order of the columns
internally and converts its column indices before querying the model.
So, when writing a TableModel, it is not necessary to listen for column
reordering events as the model will be queried in its own coordinate
system regardless of what is happening in the view.
In the examples area there is a demonstration of a sorting algorithm making
use of exactly this technique to interpose yet another coordinate system
where the order of the rows is changed, rather than the order of the columns.
As for all JComponent classes, you can use
InputMap and ActionMap to associate an
Action object with a KeyStroke and execute the
action under specified conditions.
For the keyboard keys used by this component in the standard Look and
Feel (L&F) renditions, see the
JTable key assignments.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see XMLEncoder.
| Nested Class Summary | |
protected class |
JTable.AccessibleJTable
This class implements accessibility support for the JTable class. |
| Nested classes inherited from class javax.swing.JComponent |
JComponent.AccessibleJComponent |
| Nested classes inherited from class java.awt.Container |
Container.AccessibleAWTContainer |
| Nested classes inherited from class java.awt.Component |
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| Field Summary | |
static int |
AUTO_RESIZE_ALL_COLUMNS
During all resize operations, proportionately resize all columns. |
static int |
AUTO_RESIZE_LAST_COLUMN
During all resize operations, apply adjustments to the last column only. |
static int |
AUTO_RESIZE_NEXT_COLUMN
When a column is adjusted in the UI, adjust the next column the opposite way. |
static int |
AUTO_RESIZE_OFF
Do not adjust column widths automatically; use a scrollbar. |
static int |
AUTO_RESIZE_SUBSEQUENT_COLUMNS
During UI adjustment, change subsequent columns to preserve the total width; this is the default behavior. |
protected boolean |
autoCreateColumnsFromModel
The table will query the TableModel to build the default
set of columns if this is true. |
protected int |
autoResizeMode
Determines if the table automatically resizes the width of the table's columns to take up the entire width of the table, and how it does the resizing. |
protected TableCellEditor |
cellEditor
The object that overwrites the screen real estate occupied by the current cell and allows the user to change its contents. |
protected boolean |
cellSelectionEnabled
Obsolete as of Java 2 platform v1.3. |
protected TableColumnModel |
columnModel
The TableColumnModel of the table. |
protected TableModel |
dataModel
The TableModel of the table. |
protected Hashtable |
defaultEditorsByColumnClass
A table of objects that display and edit the contents of a cell, indexed by class as declared in getColumnClass
in the TableModel interface. |
protected Hashtable |
defaultRenderersByColumnClass
A table of objects that display the contents of a cell, indexed by class as declared in getColumnClass
in the TableModel interface. |
protected int |
editingColumn
Identifies the column of the cell being edited. |
protected int |
editingRow
Identifies the row of the cell being edited. |
protected Component |
editorComp
If editing, the Component that is handling the editing. |
protected Color |
gridColor
The color of the grid. |
protected Dimension |
preferredViewportSize
Used by the Scrollable interface to determine the initial visible area. |
protected int |
rowHeight
The height in pixels of each row in the table. |
protected int |
rowMargin
The height in pixels of the margin between the cells in each row. |
protected boolean |
rowSelectionAllowed
True if row selection is allowed in this table. |
protected Color |
selectionBackground
The background color of selected cells. |
protected Color |
selectionForeground
The foreground color of selected cells. |
protected ListSelectionModel |
selectionModel
The ListSelectionModel of the table, used to keep track of row selections. |
protected boolean |
showHorizontalLines
The table draws horizontal lines between cells if showHorizontalLines is true. |
protected boolean |
showVerticalLines
The table draws vertical lines between cells if showVerticalLines is true. |
protected JTableHeader |
tableHeader
The TableHeader working with the table. |
| Fields inherited from class javax.swing.JComponent |
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| Constructor Summary | |
JTable()
Constructs a default JTable that is initialized with a default
data model, a default column model, and a default selection
model. |
|
JTable(int numRows,
int numColumns)
Constructs a JTable with numRows
and numColumns of empty cells using
DefaultTableModel. |
|
JTable(Object[][] rowData,
Object[] columnNames)
Constructs a JTable to display the values in the two dimensional array,
rowData, with column names, columnNames. |
|
JTable(TableModel dm)
Constructs a JTable that is initialized with
dm as the data model, a default column model,
and a default selection model. |
|
JTable(TableModel dm,
TableColumnModel cm)
Constructs a JTable that is initialized with
dm as the data model, cm
as the column model, and a default selection model. |
|
JTable(TableModel dm,
TableColumnModel cm,
ListSelectionModel sm)
Constructs a JTable that is initialized with
dm as the data model, cm as the
column model, and sm as the selection model. |
|
JTable(Vector rowData,
Vector columnNames)
Constructs a JTable to display the values in the
Vector of Vectors, rowData,
with column names, columnNames. |
|
| Method Summary | |
void |
addColumn(TableColumn aColumn)
Appends aColumn to the end of the array of columns held by
this JTable's column model. |
void |
addColumnSelectionInterval(int index0,
int index1)
Adds the columns from index0 to index1,
inclusive, to the current selection. |
void |
addNotify()
Calls the configureEnclosingScrollPane method. |
void |
addRowSelectionInterval(int index0,
int index1)
Adds the rows from index0 to index1, inclusive, to
the current selection. |
void |
changeSelection(int rowIndex,
int columnIndex,
boolean toggle,
boolean extend)
Updates the selection models of the table, depending on the state of the two flags: toggle and extend. |
void |
clearSelection()
Deselects all selected columns and rows. |
void |
columnAdded(TableColumnModelEvent e)
Invoked when a column is added to the table column model. |
int |
columnAtPoint(Point point)
Returns the index of the column that point lies in,
or -1 if the result is not in the range
[0, getColumnCount()-1]. |
void |
columnMarginChanged(ChangeEvent e)
Invoked when a column is moved due to a margin change. |
void |
columnMoved(TableColumnModelEvent e)
Invoked when a column is repositioned. |
void |
columnRemoved(TableColumnModelEvent e)
Invoked when a column is removed from the table column model. |
void |
columnSelectionChanged(ListSelectionEvent e)
Invoked when the selection model of the TableColumnModel
is changed. |
protected void |
configureEnclosingScrollPane()
If this JTable is the viewportView of an enclosing JScrollPane
(the usual situation), configure this ScrollPane by, amongst other things,
installing the table's tableHeader as the columnHeaderView of the scroll pane. |
int |
convertColumnIndexToModel(int viewColumnIndex)
Maps the index of the column in the view at viewColumnIndex to the index of the column
in the table model. |
int |
convertColumnIndexToView(int modelColumnIndex)
Maps the index of the column in the table model at modelColumnIndex to the index of the column
in the view. |
protected TableColumnModel |
createDefaultColumnModel()
Returns the default column model object, which is a DefaultTableColumnModel. |
void |
createDefaultColumnsFromModel()
Creates default columns for the table from the data model using the getColumnCount method
defined in the TableModel interface. |
protected TableModel |
createDefaultDataModel()
Returns the default table model object, which is a DefaultTableModel. |
protected void |
createDefaultEditors()
Creates default cell editors for objects, numbers, and boolean values. |
protected void |
createDefaultRenderers()
Creates default cell renderers for objects, numbers, doubles, dates, booleans, and icons. |
protected ListSelectionModel |
createDefaultSelectionModel()
Returns the default selection model object, which is a DefaultListSelectionModel. |
protected JTableHeader |
createDefaultTableHeader()
Returns the default table header object, which is a JTableHeader. |
static JScrollPane |
createScrollPaneForTable(JTable aTable)
Deprecated. As of Swing version 1.0.2, replaced by new JScrollPane(aTable). |
void |
doLayout()
Causes this table to lay out its rows and columns. |
boolean |
editCellAt(int row,
int column)
Programmatically starts editing the cell at row and
column, if the cell is editable. |
boolean |
editCellAt(int row,
int column,
EventObject e)
Programmatically starts editing the cell at row and
column, if the cell is editable. |
void |
editingCanceled(ChangeEvent e)
Invoked when editing is canceled. |
void |
editingStopped(ChangeEvent e)
Invoked when editing is finished. |
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JTable. |
boolean |
getAutoCreateColumnsFromModel()
Determines whether the table will create default columns from the model. |
int |
getAutoResizeMode()
Returns the auto resize mode of the table. |
TableCellEditor |
getCellEditor()
Returns the cell editor. |
TableCellEditor |
getCellEditor(int row,
int column)
Returns an appropriate editor for the cell specified by row and column. |
Rectangle |
getCellRect(int row,
int column,
boolean includeSpacing)
Returns a rectangle for the cell that lies at the intersection of row and column. |
TableCellRenderer |
getCellRenderer(int row,
int column)
Returns an appropriate renderer for the cell specified by this row and column. |
boolean |
getCellSelectionEnabled()
Returns true if both row and column selection models are enabled. |
TableColumn |
getColumn(Object identifier)
Returns the TableColumn object for the column in the table
whose identifier is equal to identifier, when compared using
equals. |
Class |
getColumnClass(int column)
Returns the type of the column appearing in the view at column position column. |
int |
getColumnCount()
Returns the number of columns in the column model. |
TableColumnModel |
getColumnModel()
Returns the TableColumnModel that contains all column information
of this table. |
String |
getColumnName(int column)
Returns the name of the column appearing in the view at column position column. |
boolean |
getColumnSelectionAllowed()
Returns true if columns can be selected. |
TableCellEditor |
getDefaultEditor(Class columnClass)
Returns the editor to be used when no editor has been set in a TableColumn. |
TableCellRenderer |
getDefaultRenderer(Class columnClass)
Returns the cell renderer to be used when no renderer has been set in a TableColumn. |
boolean |
getDragEnabled()
Gets the value of the dragEnabled property. |
int |
getEditingColumn()
Returns the index of the column that contains the cell currently being edited. |
int |
getEditingRow()
Returns the index of the row that contains the cell currently being edited. |
Component |
getEditorComponent()
Returns the component that is handling the editing session. |
Color |
getGridColor()
Returns the color used to draw grid lines. |
Dimension |
getIntercellSpacing()
Returns the horizontal and vertical space between cells. |
TableModel |
getModel()
Returns the TableModel that provides the data displayed by this
JTable. |
Dimension |
getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for this table. |
int |
getRowCount()
Returns the number of rows in this table's model. |
int |
getRowHeight()
Returns the height of a table row, in pixels. |
int |
getRowHeight(int row)
Returns the height, in pixels, of the cells in row. |
int |
getRowMargin()
Gets the amount of empty space, in pixels, between cells. |
boolean |
getRowSelectionAllowed()
Returns true if rows can be selected. |
int |
getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns visibleRect.height or
visibleRect.width,
depending on this table's orientation. |
boolean |
getScrollableTracksViewportHeight()
Returns false to indicate that the height of the viewport does not determine the height of the table. |
boolean |
getScrollableTracksViewportWidth()
Returns false if autoResizeMode is set to
AUTO_RESIZE_OFF, which indicates that the
width of the viewport does not determine the width
of the table. |
int |
getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns the scroll increment (in pixels) that completely exposes one new row or column (depending on the orientation). |
int |
getSelectedColumn()
Returns the index of the first selected column, -1 if no column is selected. |
int |
getSelectedColumnCount()
Returns the number of selected columns. |
int[] |
getSelectedColumns()
Returns the indices of all selected columns. |
int |
getSelectedRow()
Returns the index of the first selected row, -1 if no row is selected. |
int |
getSelectedRowCount()
Returns the number of selected rows. |
int[] |
getSelectedRows()
Returns the indices of all selected rows. |
Color |
getSelectionBackground()
Returns the background color for selected cells. |
Color |
getSelectionForeground()
Returns the foreground color for selected cells. |
ListSelectionModel |
getSelectionModel()
Returns the ListSelectionModel that is used to maintain row
selection state. |
boolean |
getShowHorizontalLines()
Returns true if the table draws horizontal lines between cells, false if it doesn't. |
boolean |
getShowVerticalLines()
Returns true if the table draws vertical lines between cells, false if it doesn't. |
boolean |
getSurrendersFocusOnKeystroke()
Returns true if the editor should get the focus when keystrokes cause the editor to be activated |
JTableHeader |
getTableHeader()
Returns the tableHeader used by this JTable. |
String |
getToolTipText(MouseEvent event)
Overrides JComponent's getToolTipText
method in order to allow the renderer's tips to be used
if it has text set. |
TableUI |
getUI()
Returns the L&F object that renders this component. |
String |
getUIClassID()
Returns the suffix used to construct the name of the L&F class used to render this component. |
Object |
getValueAt(int row,
int column)
Returns the cell value at row and column. |
protected void |
initializeLocalVars()
Initializes table properties to their default values. |
boolean |
isCellEditable(int row,
int column)
Returns true if the cell at row and column
is editable. |
boolean |
isCellSelected(int row,
int column)
Returns true if the cell at the specified position is selected. |
boolean |
isColumnSelected(int column)
Returns true if the column at the specified index is selected. |
boolean |
isEditing()
Returns true if a cell is being edited. |
boolean |
isRowSelected(int row)
Returns true if the row at the specified index is selected. |
void |
moveColumn(int column,
int targetColumn)
Moves the column column to the position currently
occupied by the column targetColumn in the view. |
protected String |
paramString()
Returns a string representation of this table. |
Component |
prepareEditor(TableCellEditor editor,
int row,
int column)
Prepares the editor by querying the data model for the value and selection state of the cell at row, column. |
Component |
prepareRenderer(TableCellRenderer renderer,
int row,
int column)
Prepares the renderer by querying the data model for the value and selection state of the cell at row, column. |
protected boolean |
processKeyBinding(KeyStroke ks,
KeyEvent e,
int condition,
boolean pressed)
Invoked to process the key bindings for ks as the result
of the KeyEvent e. |
void |
removeColumn(TableColumn aColumn)
Removes aColumn from this JTable's
array of columns. |
void |
removeColumnSelectionInterval(int index0,
int index1)
Deselects the columns from index0 to index1, inclusive. |
void |
removeEditor()
Discards the editor object and frees the real estate it used for cell rendering. |
void |
removeNotify()
Calls the unconfigureEnclosingScrollPane method. |
void |
removeRowSelectionInterval(int index0,
int index1)
Deselects the rows from index0 to index1, inclusive. |
protected void |
resizeAndRepaint()
Equivalent to revalidate followed by repaint. |
int |
rowAtPoint(Point point)
Returns the index of the row that point lies in,
or -1 if the result is not in the range
[0, getRowCount()-1]. |
void |
selectAll()
Selects all rows, columns, and cells in the table. |
void |
setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel)
Sets this table's autoCreateColumnsFromModel flag. |
void |
setAutoResizeMode(int mode)
Sets the table's auto resize mode when the table is resized. |
void |
setCellEditor(TableCellEditor anEditor)
Sets the cellEditor variable. |
void |
setCellSelectionEnabled(boolean cellSelectionEnabled)
Sets whether this table allows both a column selection and a row selection to exist simultaneously. |
void |
setColumnModel(TableColumnModel columnModel)
Sets the column model for this table to newModel and registers
for listener notifications from the new column model. |
void |
setColumnSelectionAllowed(boolean columnSelectionAllowed)
Sets whether the columns in this model can be selected. |
void |
setColumnSelectionInterval(int index0,
int index1)
Selects the columns from index0 to index1,
inclusive. |
void |
setDefaultEditor(Class columnClass,
TableCellEditor editor)
Sets a default cell editor to be used if no editor has been set in a TableColumn. |
void |
setDefaultRenderer(Class columnClass,
TableCellRenderer renderer)
Sets a default cell renderer to be used if no renderer has been set in a TableColumn. |
void |
setDragEnabled(boolean b)
Sets the dragEnabled property,
which must be true to enable
automatic drag handling (the first part of drag and drop)
on this component. |
void |
setEditingColumn(int aColumn)
Sets the editingColumn variable. |
void |
setEditingRow(int aRow)
Sets the editingRow variable. |
void |
setGridColor(Color gridColor)
Sets the color used to draw grid lines to gridColor and redisplays. |
void |
setIntercellSpacing(Dimension intercellSpacing)
Sets the rowMargin and the columnMargin --
the height and width of the space between cells -- to
intercellSpacing. |
void |
setModel(TableModel dataModel)
Sets the data model for this table to newModel and registers
with it for listener notifications from the new data model. |
void |
setPreferredScrollableViewportSize(Dimension size)
Sets the preferred size of the viewport for this table. |
void |
setRowHeight(int rowHeight)
Sets the height, in pixels, of all cells to rowHeight,
revalidates, and repaints. |
void |
setRowHeight(int row,
int rowHeight)
Sets the height for row to rowHeight,
revalidates, and repaints. |
void |
setRowMargin(int rowMargin)
Sets the amount of empty space between cells in adjacent rows. |
void |
setRowSelectionAllowed(boolean rowSelectionAllowed)
Sets whether the rows in this model can be selected. |
void |
setRowSelectionInterval(int index0,
int index1)
Selects the rows from index0 to index1,
inclusive. |
void |
setSelectionBackground(Color selectionBackground)
Sets the background color for selected cells. |
void |
setSelectionForeground(Color selectionForeground)
Sets the foreground color for selected cells. |
void |
setSelectionMode(int selectionMode)
Sets the table's selection mode to allow only single selections, a single contiguous interval, or multiple intervals. |
void |
setSelectionModel(ListSelectionModel newModel)
Sets the row selection model for this table to newModel
and registers for listener notifications from the new selection model. |
void |
setShowGrid(boolean showGrid)
Sets whether the table draws grid lines around cells. |
void |
setShowHorizontalLines(boolean showHorizontalLines)
Sets whether the table draws horizontal lines between cells. |
void |
setShowVerticalLines(boolean showVerticalLines)
Sets whether the table draws vertical lines between cells. |
void |
setSurrendersFocusOnKeystroke(boolean surrendersFocusOnKeystroke)
Sets whether editors in this JTable get the keyboard focus when an editor is activated as a result of the JTable forwarding keyboard events for a cell. |
void |
setTableHeader(JTableHeader tableHeader)
Sets the tableHeader working with this JTable to newHeader. |
void |
setUI(TableUI ui)
Sets the L&F object that renders this component and repaints. |
void |
setValueAt(Object aValue,
int row,
int column)
Sets the value for the cell in the table model at row
and column. |
void |
sizeColumnsToFit(boolean lastColumnOnly)
Deprecated. As of Swing version 1.0.3, replaced by doLayout(). |
void |
sizeColumnsToFit(int resizingColumn)
Obsolete as of Java 2 platform v1.4. |
void |
tableChanged(TableModelEvent e)
Invoked when this table's TableModel generates
a TableModelEvent. |
protected void |
unconfigureEnclosingScrollPane()
Reverses the effect of configureEnclosingScrollPane
by replacing the columnHeaderView of the enclosing
scroll pane with null. |
void |
updateUI()
Notification from the UIManager that the L&F has changed. |
void |
valueChanged(ListSelectionEvent e)
Invoked when the row selection changes -- repaints to show the new selection. |
| Methods inherited from class java.awt.Component |
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, |