javax.swing

Class JTable

Implemented Interfaces:
Accessible, CellEditorListener, EventListener, ImageObserver, ListSelectionListener, MenuContainer, Scrollable, Serializable, TableColumnModelListener, TableModelListener

public class JTable
extends JComponent
implements TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible

The table component, displaying information, organized in rows and columns. The table can be placed in the scroll bar and have the optional header that is always visible. Cell values may be editable after double clicking on the cell. Cell columns may have various data types, that are displayed and edited by the different renderers and editors. It is possible to set different column width. The columns are also resizeable by dragging the column boundary in the header.
See Also:
Serialized Form

Nested Class Summary

protected class
JTable.AccessibleJTable
Provides accessibility support for JTable.

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, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

static int
AUTO_RESIZE_ALL_COLUMNS
When resizing column i in a table of n columns, automatically change all columns in the range [0, n) (with the exception of column i) uniformly, to provide or absorb excess space requirements.
static int
AUTO_RESIZE_LAST_COLUMN
When resizing column i in a table of n columns, automatically change column n-1 (the last column in the table) to provide or absorb excess space requirements.
static int
AUTO_RESIZE_NEXT_COLUMN
When resizing column i, automatically change only the single column i+1 to provide or absorb excess space requirements.
static int
AUTO_RESIZE_OFF
When resizing columns, do not automatically change any columns.
static int
AUTO_RESIZE_SUBSEQUENT_COLUMNS
When resizing column i in a table of n columns, automatically change all columns in the range [i+1, n), uniformly, to provide or absorb excess space requirements.
protected boolean
autoCreateColumnsFromModel
Whether or not the table should automatically compute a matching TableColumnModel and assign it to the columnModel property when the dataModel property is changed.
protected int
autoResizeMode
A numeric code specifying the resizing behavior of the table.
protected TableCellEditor
cellEditor
The current cell editor.
protected boolean
cellSelectionEnabled
Obsolete.
protected TableColumnModel
columnModel
A model of various aspects of the columns of the table, not including the data stored in them.
protected TableModel
dataModel
The model for data stored in the table.
protected Hashtable<K,V>
defaultEditorsByColumnClass
A table mapping Class objects to TableCellEditor objects.
protected Hashtable<K,V>
defaultRenderersByColumnClass
A table mapping Class objects to TableCellEditor objects.
protected int
editingColumn
The column that is edited, -1 if the table is not edited currently.
protected int
editingRow
The row that is edited, -1 if the table is not edited currently.
protected Component
editorComp
The component that is used for editing.
protected Color
gridColor
The color to paint the grid lines of the table, when either showHorizontalLines or showVerticalLines is set.
protected Dimension
preferredViewportSize
The size this table would prefer its viewport assume, if it is contained in a JScrollPane.
protected int
rowHeight
The height in pixels of any row of the table.
protected int
rowMargin
The height in pixels of the gap left between any two rows of the table.
protected boolean
rowSelectionAllowed
Whether or not the table should allow row selection.
protected Color
selectionBackground
The color to paint the background of selected cells.
protected Color
selectionForeground
The color to paint the foreground of selected cells.
protected ListSelectionModel
selectionModel
A model of the rows of this table which are currently selected.
protected boolean
showHorizontalLines
The showHorizontalLines property.
protected boolean
showVerticalLines
The showVerticalLines property.
protected JTableHeader
tableHeader
The tableHeader property.

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

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()
Creates a new JTable instance.
JTable(int numRows, int numColumns)
Creates a new JTable instance with the given number of rows and columns.
JTable(Object[][] data, Object[] columnNames)
Creates a new JTable instance, storing the given data array and heaving the given column names.
JTable(Vector<T> data, Vector<T> columnNames)
Creates a new JTable instance that uses data and column names, stored in Vectors.
JTable(TableModel model)
Creates a new JTable instance, using the given data model object that provides information about the table content.
JTable(TableModel dm, TableColumnModel cm)
Creates a new JTable instance, using the given model object that provides information about the table content.
JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
Creates a new JTable instance, providing data model, column model and list selection model.

Method Summary

void
addColumn(TableColumn column)
Add the new table column.
void
addColumnSelectionInterval(int index0, int index1)
void
addNotify()
Called when this container is added to another container to inform it to create its peer.
void
addRowSelectionInterval(int index0, int index1)
void
changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
void
clearSelection()
void
columnAdded(TableColumnModelEvent event)
Invoked when the column is added.
int
columnAtPoint(Point point)
Returns index of the column that contains specified point or -1 if this table doesn't contain this point.
void
columnMarginChanged(ChangeEvent event)
Invoked when the column margin is changed.
void
columnMoved(TableColumnModelEvent event)
Invoked when the column is moved.
void
columnRemoved(TableColumnModelEvent event)
Invoked when the column is removed.
void
columnSelectionChanged(ListSelectionEvent event)
Invoked when the the column selection changes, repaints the changed columns.
protected void
configureEnclosingScrollPane()
int
convertColumnIndexToModel(int vc)
Convert the table model index into the table column number.
int
convertColumnIndexToView(int mc)
Convert the table column number to the table column model index.
protected TableColumnModel
createDefaultColumnModel()
Create the default table column model that is used if the user-defined column model is not provided.
void
createDefaultColumnsFromModel()
Clears any existing columns from the JTable's TableColumnModel and creates new columns to match the values in the data (TableModel) used by the table.
protected TableModel
createDefaultDataModel()
Create the default table data model that is used if the user-defined data model is not provided.
protected void
createDefaultEditors()
Create the default editors for this table.
protected void
createDefaultRenderers()
Create the default renderers for this table.
protected ListSelectionModel
createDefaultSelectionModel()
Create the default table selection model that is used if the user-defined selection model is not provided.
protected JTableHeader
createDefaultTableHeader()
Create the default table header, if the user - defined table header is not provided.
static JScrollPane
createScrollPaneForTable(JTable table)
Deprecated. 1.0.2, replaced by new JScrollPane(JTable)
void
doLayout()
Set the widths of all columns, taking they preferred widths into consideration.
boolean
editCellAt(int row, int column)
Programmatically starts editing the specified cell.
boolean
editCellAt(int row, int column, EventObject e)
Programmatically starts editing the specified cell.
void
editingCanceled(ChangeEvent event)
Invoked when the editing is cancelled.
void
editingStopped(ChangeEvent event)
Finish the current editing session and update the table with the new value by calling setValueAt(Object,int,int).
AccessibleContext
getAccessibleContext()
Get the value of the JComponent.accessibleContext property.
boolean
getAutoCreateColumnsFromModel()
Get the value of the autoCreateColumnsFromModel property.
int
getAutoResizeMode()
Get the value of the autoResizeMode property.
TableCellEditor
getCellEditor()
Get the value of the cellEditor property.
TableCellEditor
getCellEditor(int row, int column)
Get the cell editor, suitable for editing the given cell.
Rectangle
getCellRect(int row, int column, boolean includeSpacing)
Calculate the visible rectangle for a particular row and column.
TableCellRenderer
getCellRenderer(int row, int column)
Get the cell renderer for rendering the given cell.
boolean
getCellSelectionEnabled()
Get the value of the cellSelectionEnabled property.
TableColumn
getColumn(Object identifier)
Get table column with the given identified.
Class
getColumnClass(int column)
Get the class (datatype) of the column.
int
getColumnCount()
Get the value of the columnCount property by delegation to the columnModel field.
TableColumnModel
getColumnModel()
Get the value of the columnModel property.
String
getColumnName(int column)
Get the name of the column.
boolean
getColumnSelectionAllowed()
Get the value of the columnSelectionAllowed property.
TableCellEditor
getDefaultEditor(Class columnClass)
Get the default editor for editing values of the given type (String, Boolean and so on).
TableCellRenderer
getDefaultRenderer(Class columnClass)
Get the default renderer for rendering the given data type.
boolean
getDragEnabled()
Get the value of the dragEnabled property.
int
getEditingColumn()
Get the column, currently being edited
int
getEditingRow()
Get the row currently being edited.
Component
getEditorComponent()
Get the editor component that is currently editing one of the cells
Color
getGridColor()
Get the value of the gridColor property.
Dimension
getIntercellSpacing()
Get the value of the intercellSpacing property.
TableModel
getModel()
Get the value of the dataModel property.
Dimension
getPreferredScrollableViewportSize()
Get the value of the preferredViewportSize property.
int
getRowCount()
Get the value of the rowCount property by delegation to the dataModel field.
int
getRowHeight()
Get the value of the rowHeight property.
int
getRowHeight(int row)
Get the height of the specified row.
int
getRowMargin()
Get the value of the rowMargin property.
boolean
getRowSelectionAllowed()
Get the value of the rowSelectionAllowed property.
int
getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
boolean
getScrollableTracksViewportHeight()
Get the value of the scrollableTracksViewportHeight property.
boolean
getScrollableTracksViewportWidth()
Get the value of the scrollableTracksViewportWidth property.
int
getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
Return the preferred scrolling amount (in pixels) for the given scrolling direction and orientation.
int
getSelectedColumn()
Get the value of the selectedColumn property by delegation to the columnModel field.
int
getSelectedColumnCount()
Get the value of the selectedColumnCount property by delegation to the columnModel field.
int[]
getSelectedColumns()
Get the value of the selectedColumns property by delegation to the columnModel field.
int
getSelectedRow()
Get the value of the selectedRow property by delegation to the ListSelectionModel.getMinSelectionIndex() method of the selectionModel field.
int
getSelectedRowCount()
Get the value of the selectedRowCount property by delegation to the selectionModel field.
int[]
getSelectedRows()
Get the value of the selectedRows property by delegation to the selectionModel field.
Color
getSelectionBackground()
Get the value of the selectionBackground property.
Color
getSelectionForeground()
Get the value of the selectionForeground property.
ListSelectionModel
getSelectionModel()
Get the value of the selectionModel property.
boolean
getShowHorizontalLines()
Get the value of the showHorizontalLines property.
boolean
getShowVerticalLines()
Get the value of the showVerticalLines property.
boolean
getSurrendersFocusOnKeystroke()
Returns whether cell editors of this table should receive keyboard focus when the editor is activated by a keystroke.
JTableHeader
getTableHeader()
Get the value of the tableHeader property.
TableUI
getUI()
This method returns the table's UI delegate.
String
getUIClassID()
Object
getValueAt(int row, int column)
Get the cell value at the given position.
protected void
initializeLocalVars()
Initialize local variables to default values.
boolean
isCellEditable(int row, int column)
Returns true if the specified cell is editable, and false otherwise.
boolean
isCellSelected(int row, int column)
Checks if the given cell is selected.
boolean
isColumnSelected(int column)
Checks if the given column is selected.
boolean
isEditing()
Check if one of the table cells is currently being edited.
boolean
isRowSelected(int row)
Checks if the given row is selected.
void
moveColumn(int column, int targetColumn)
Moves column at the specified index to new given location.
Component
prepareEditor(TableCellEditor editor, int row, int column)
Prepares the editor by querying for the value and selection state of the cell at (row, column).
Component
prepareRenderer(TableCellRenderer renderer, int row, int column)
Prepare the renderer for rendering the given cell.
void
removeColumn(TableColumn column)
Removes specified column from displayable columns of this table.
void
removeColumnSelectionInterval(int index0, int index1)
void
removeEditor()
Discards the editor object.
void
removeNotify()
Called when this container is removed from its parent container to inform it to destroy its peer.
void
removeRowSelectionInterval(int index0, int index1)
protected void
resizeAndRepaint()
This revalidates the JTable and queues a repaint.
int
rowAtPoint(Point point)
Returns index of the row that contains specified point or -1 if this table doesn't contain this point.
void
selectAll()
Select all table.
void
setAutoCreateColumnsFromModel(boolean autoCreate)
Set the value of the autoCreateColumnsFromModel flag.
void
setAutoResizeMode(int a)
Set the value of the autoResizeMode property.
void
setCellEditor(TableCellEditor c)
Set the value of the cellEditor property.

Unregister this as a CellEditorListener from previous cellEditor and register it with new parameter c.

void
setCellSelectionEnabled(boolean c)
Set the value of the cellSelectionEnabled property.
void
setColumnModel(TableColumnModel c)
Set the value of the columnModel property.

Unregister this as a TableColumnModelListener from previous columnModel and register it with new parameter c.

void
setColumnSelectionAllowed(boolean c)
Set the value of the columnSelectionAllowed property.
void
setColumnSelectionInterval(int index0, int index1)
void
setDefaultEditor(Class columnClass, TableCellEditor editor)
Set the default editor for the given column class (column data type).
void
setDefaultRenderer(Class columnClass, TableCellRenderer rend)
Set default renderer for rendering the given data type.
void
setDragEnabled(boolean d)
Set the value of the dragEnabled property.
void
setEditingColumn(int column)
Set the column, currently being edited
void
setEditingRow(int row)
Set the row currently being edited.
void
setGridColor(Color g)
Set the value of the gridColor property.
void
setIntercellSpacing(Dimension i)
Set the value of the intercellSpacing property.
void
setModel(TableModel m)
Set the value of the dataModel property.

Unregister this as a TableModelListener from previous dataModel and register it with new parameter m.

void
setPreferredScrollableViewportSize(Dimension p)
Set the value of the preferredViewportSize property.
void
setRowHeight(int r)
Sets the height for all rows in the table.
void
setRowHeight(int row, int rh)
Sets the height of a single row in the table.
void
setRowMargin(int r)
Set the value of the rowMargin property.
void
setRowSelectionAllowed(boolean r)
Set the value of the rowSelectionAllowed property.
void
setRowSelectionInterval(int index0, int index1)
void
setSelectionBackground(Color s)
Set the value of the selectionBackground property.

Fire a PropertyChangeEvent with name SELECTION_BACKGROUND_CHANGED_PROPERTY to registered listeners, if selectionBackground changed.

void
setSelectionForeground(Color s)
Set the value of the selectionForeground property.

Fire a PropertyChangeEvent with name SELECTION_FOREGROUND_CHANGED_PROPERTY to registered listeners, if selectionForeground changed.

void
setSelectionMode(int s)
Set the value of the selectionMode property by delegation to the selectionModel field.
void
setSelectionModel(ListSelectionModel s)
Set the value of the selectionModel property.

Unregister this as a ListSelectionListener from previous selectionModel and register it with new parameter s.

void
setShowGrid(boolean s)
Set the value of the showGrid property.
void
setShowHorizontalLines(boolean s)
Set the value of the showHorizontalLines property.
void
setShowVerticalLines(boolean s)
Set the value of the showVerticalLines property.
void
setSurrendersFocusOnKeystroke(boolean value)
Sets whether cell editors of this table should receive keyboard focus when the editor is activated by a keystroke.
void
setTableHeader(JTableHeader t)
Set the value of the tableHeader property.
void
setUI(TableUI ui)
This method sets the table's UI delegate.
void
setValueAt(Object value, int row, int column)
Set value for the cell at the given position.
void
sizeColumnsToFit(boolean lastColumnOnly)
Deprecated. Replaced by doLayout()
void
sizeColumnsToFit(int resizingColumn)
Obsolete since JDK 1.4.
void
tableChanged(TableModelEvent event)
Invoked when the table changes.
protected void
unconfigureEnclosingScrollPane()
void
updateUI()
void
valueChanged(ListSelectionEvent event)
Invoked when another table row is selected.

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize,