|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.awt.Component
![]()
![]()
![]()
java.awt.Container
![]()
![]()
![]()
javax.swing.JComponent
![]()
![]()
![]()
javax.swing.JTable
, MenuContainer
, Serializable
, EventListener
, Accessible
, CellEditorListener
, ListSelectionListener
, TableColumnModelListener
, TableModelListener
, Scrollable

public class JTable

, Scrollable
, TableColumnModelListener
, ListSelectionListener
, CellEditorListener
, Accessible

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.
J2SE 5 adds methods to JTable to provide convenient access to some
common printing needs. Simple new print()
methods allow for quick
and easy addition of printing support to your application. In addition, a new
getPrintable(javax.swing.JTable.PrintMode, java.text.MessageFormat, java.text.MessageFormat)
method is available for more advanced printing needs.
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.
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. |
static class |
JTable.PrintMode
Printing modes, used in printing JTables. |
Nested classes/interfaces inherited from class javax.swing.JComponent ![]() |
|---|
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container ![]() |
|---|
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component ![]() |
|---|
Component.AccessibleAWTComponent |
| 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 |
Fields inherited from class java.awt.Component ![]() |
|---|
BOTTOM_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver ![]() |
|---|
ABORT |
| 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
Constructs a JTable with numRows
and numColumns of empty cells using
DefaultTableModel. |
|
JTable
Constructs a JTable to display the values in the two dimensional array,
rowData, with column names, columnNames. |
|
JTable
Constructs a JTable that is initialized with
dm as the data model, a default column model,
and a default selection model. |
|
JTable
Constructs a JTable that is initialized with
dm as the data model, cm
as the column model, and a default selection model. |
|
JTable
Constructs a JTable that is initialized with
dm as the data model, cm as the
column model, and sm as the selection model. |
|
JTable
Constructs a JTable to display the values in the
Vector of Vectors, rowData,
with column names, columnNames. |
|
| Method Summary | |
|---|---|
void |
addColumn
Appends aColumn to the end of the array of columns held by
this JTable's column model. |
void |
addColumnSelectionInterval
Adds the columns from index0 to index1,
inclusive, to the current selection. |
void |
addNotify
Calls the configureEnclosingScrollPane method. |
void |
addRowSelectionInterval
Adds the rows from index0 to index1, inclusive, to
the current selection. |
void |
changeSelection
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
Invoked when a column is added to the table column model. |
int |
columnAtPoint
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
Invoked when a column is moved due to a margin change. |
void |
columnMoved
Invoked when a column is repositioned. |
void |
columnRemoved
Invoked when a column is removed from the table column model. |
void |
columnSelectionChanged
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
Maps the index of the column in the view at viewColumnIndex to the index of the column
in the table model. |
int |
convertColumnIndexToView
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 |