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

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
Deprecated. Use rowSelectionAllowed, getColumnSelectionAllowed(), or the combined methods getCellSelectionEnabled() and setCellSelectionEnabled(boolean).
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
defaultEditorsByColumnClass
A table mapping Class objects to TableCellEditor objects.
protected Hashtable
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.
JTable(Object[][] data, Object[] columnNames)
Creates a new JTable instance.
JTable(Vector data, Vector columnNames)
Creates a new JTable instance.
JTable(TableModel dm)
Creates a new JTable instance.
JTable(TableModel dm, TableColumnModel cm)
Creates a new JTable instance.
JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
Creates a new JTable instance.

Method Summary

void
addColumn(TableColumn 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)
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)
void
columnMoved(TableColumnModelEvent event)
void
columnRemoved(TableColumnModelEvent event)
void
columnSelectionChanged(ListSelectionEvent event)
protected void
configureEnclosingScrollPane()
int
convertColumnIndexToModel(int vc)
int
convertColumnIndexToView(int mc)
protected TableColumnModel
createDefaultColumnModel()
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()
protected void
createDefaultEditors()
protected void
createDefaultRenderers()
protected ListSelectionModel
createDefaultSelectionModel()
protected JTableHeader
createDefaultTableHeader()
static JScrollPane
createScrollPaneForTable(JTable table)
Deprecated. 1.0.2, replaced by new JScrollPane(JTable)
void
doLayout()
Layout the components in this container.
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)
void
editingStopped(ChangeEvent event)
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)
Rectangle
getCellRect(int row, int column, boolean includeSpacing)
Calculate the visible rectangle for a particular row and column.
TableCellRenderer
getCellRenderer(int row, int column)
boolean
getCellSelectionEnabled()
Get the value of the cellSelectionEnabled property.
TableColumn
getColumn(Object identifier)
Class
getColumnClass(int column)
int
getColumnCount()
Get the value of the columnCount property by delegation to the @{link #columnModel} field.
TableColumnModel
getColumnModel()
Get the value of the columnModel property.
String
getColumnName(int column)
boolean
getColumnSelectionAllowed()
Get the value of the columnSelectionAllowed property.
TableCellEditor
getDefaultEditor(Class columnClass)
TableCellRenderer
getDefaultRenderer(Class columnClass)
boolean
getDragEnabled()
Get the value of the dragEnabled property.
int
getEditingColumn()
int
getEditingRow()
Component
getEditorComponent()
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 @{link #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)
int
getSelectedColumn()
Get the value of the selectedColumn property by delegation to the @{link #columnModel} field.
int
getSelectedColumnCount()
Get the value of the selectedColumnCount property by delegation to the @{link #columnModel} field.
int[]
getSelectedColumns()
Get the value of the selectedColumns property by delegation to the @{link #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 @{link #selectionModel} field.
int[]
getSelectedRows()
Get the value of the selectedRows property by delegation to the @{link #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)
protected void
initializeLocalVars()
boolean
isCellEditable(int row, int column)
Returns true if the specified cell is editable, and false otherwise.
boolean
isCellSelected(int row, int column)
boolean
isColumnSelected(int column)
boolean
isEditing()
boolean
isRowSelected(int row)
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)
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()
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)
void
setDefaultRenderer(Class columnClass, TableCellRenderer rend)
void
setDragEnabled(boolean d)
Set the value of the dragEnabled property.
void
setEditingColumn(int column)
void
setEditingRow(int column)
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)
Set the value of the rowHeight property.
void
setRowHeight(int row, int rh)
Sets the value of the rowHeight property for the specified row.
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)
void
sizeColumnsToFit(boolean lastColumnOnly)
Deprecated. Replaced by doLayout()
void
sizeColumnsToFit(int resizingColumn)
Obsolete since JDK 1.4.
void
tableChanged(TableModelEvent event)
protected void
unconfigureEnclosingScrollPane()
void
updateUI()
void
valueChanged(ListSelectionEvent event)

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, 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, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, setLayout, swapComponents, 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, 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, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

AUTO_RESIZE_ALL_COLUMNS

public static final 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.
Field Value:
4

AUTO_RESIZE_LAST_COLUMN

public static final 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.
Field Value:
3

AUTO_RESIZE_NEXT_COLUMN

public static final int AUTO_RESIZE_NEXT_COLUMN
When resizing column i, automatically change only the single column i+1 to provide or absorb excess space requirements.
Field Value:
1

AUTO_RESIZE_OFF

public static final int AUTO_RESIZE_OFF
When resizing columns, do not automatically change any columns. In this case the table should be enclosed in a JScrollPane in order to accomodate cases in which the table size exceeds its visible area.
Field Value:
0

AUTO_RESIZE_SUBSEQUENT_COLUMNS

public static final 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.
Field Value:
2

autoCreateColumnsFromModel

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.

autoResizeMode

protected int autoResizeMode
A numeric code specifying the resizing behavior of the table. Must be one of AUTO_RESIZE_ALL_COLUMNS (the default), AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS, or AUTO_RESIZE_OFF.

cellEditor

protected TableCellEditor cellEditor
The current cell editor.

cellSelectionEnabled

protected boolean cellSelectionEnabled

Deprecated. Use rowSelectionAllowed, getColumnSelectionAllowed(), or the combined methods getCellSelectionEnabled() and setCellSelectionEnabled(boolean).


columnModel

protected TableColumnModel columnModel
A model of various aspects of the columns of the table, not including the data stored in them. The TableColumnModel is principally concerned with holding a set of TableColumn objects, each of which describes the display parameters of a column and the numeric index of the column from the data model which the column is presenting.

The TableColumnModel also contains a ListSelectionModel which indicates which columns are currently selected. This selection model works in combination with the selectionModel of the table itself to specify a table selection: a combination of row and column selections.

Most application programmers do not need to work with this property at all: setting autoCreateColumnsFromModel will construct the columnModel automatically, and the table acts as a facade for most of the interesting properties of the columnModel anyways.


dataModel

protected TableModel dataModel
The model for data stored in the table. Confusingly, the published API requires that this field be called dataModel, despite its property name. The table listens to its model as a TableModelListener.

defaultEditorsByColumnClass

protected Hashtable defaultEditorsByColumnClass
A table mapping Class objects to TableCellEditor objects. This table is consulted by the FIXME

defaultRenderersByColumnClass

protected Hashtable defaultRenderersByColumnClass
A table mapping Class objects to TableCellEditor objects. This table is consulted by the FIXME

editingColumn

protected int editingColumn
The column that is edited, -1 if the table is not edited currently.

editingRow

protected int editingRow
The row that is edited, -1 if the table is not edited currently.

editorComp

protected Component editorComp
The component that is used for editing. null if the table is not editing currently.

gridColor

protected Color gridColor
The color to paint the grid lines of the table, when either showHorizontalLines or showVerticalLines is set.

preferredViewportSize

protected Dimension preferredViewportSize
The size this table would prefer its viewport assume, if it is contained in a JScrollPane.

rowHeight

protected int rowHeight
The height in pixels of any row of the table. All rows in a table are of uniform height. This differs from column width, which varies on a per-column basis, and is stored in the individual columns of the columnModel.

rowMargin

protected int rowMargin
The height in pixels of the gap left between any two rows of the table.

rowSelectionAllowed

protected boolean rowSelectionAllowed
Whether or not the table should allow row selection. If the table allows both row and column selection, it is said to allow "cell selection". Previous versions of the JDK supported cell selection as an independent concept, but it is now represented solely in terms of simultaneous row and column selection.

selectionBackground

protected Color selectionBackground
The color to paint the background of selected cells. Fires a property change event with name SELECTION_BACKGROUND_CHANGED_PROPERTY when its value changes.

selectionForeground

protected Color selectionForeground
The color to paint the foreground of selected cells. Fires a property change event with name SELECTION_FOREGROUND_CHANGED_PROPERTY when its value changes.

selectionModel

protected ListSelectionModel selectionModel
A model of the rows of this table which are currently selected. This model is used in combination with the column selection model held as a member of the columnModel property, to represent the rows and columns (or both: cells) of the table which are currently selected.

showHorizontalLines

protected boolean showHorizontalLines
The showHorizontalLines property.

showVerticalLines

protected boolean showVerticalLines
The showVerticalLines property.

tableHeader

protected JTableHeader tableHeader
The tableHeader property.

Constructor Details

JTable

public JTable()
Creates a new JTable instance.

JTable

public JTable(int numRows,
              int numColumns)
Creates a new JTable instance.
Parameters:
numRows - an int value
numColumns - an int value

JTable

public JTable(Object[][] data,
              Object[] columnNames)
Creates a new JTable instance.
Parameters:
data - an Object[][] value
columnNames - an Object[] value

JTable

public JTable(Vector data,
              Vector columnNames)
Creates a new JTable instance.
Parameters:
data - a Vector value
columnNames - a Vector value

JTable

public JTable(TableModel dm)
Creates a new JTable instance.
Parameters:
dm - a TableModel value

JTable

public JTable(TableModel dm,
              TableColumnModel cm)
Creates a new JTable instance.
Parameters:
dm - a TableModel value
cm - a TableColumnModel value

JTable

public JTable(TableModel dm,
              TableColumnModel cm,
              ListSelectionModel sm)
Creates a new JTable instance.
Parameters:
dm - a TableModel value
cm - a TableColumnModel value
sm - a ListSelectionModel value

Method Details

addColumn

public void addColumn(TableColumn column)

addColumnSelectionInterval

public void addColumnSelectionInterval(int index0,
                                       int index1)

addNotify

public void addNotify()
Called when this container is added to another container to inform it to create its peer. Peers for any child components will also be created.
Overrides:
addNotify in interface JComponent

addRowSelectionInterval

public void addRowSelectionInterval(int index0,
                                    int index1)

changeSelection

public void changeSelection(int rowIndex,
                            int columnIndex,
                            boolean toggle,
                            boolean extend)

clearSelection

public void clearSelection()

columnAdded

public void columnAdded(TableColumnModelEvent event)
Specified by:
columnAdded in interface TableColumnModelListener

columnAtPoint

public int columnAtPoint(Point point)
Returns index of the column that contains specified point or -1 if this table doesn't contain this point.
Parameters:
point - point to identify the column
Returns:
index of the column that contains specified point or -1 if this table doesn't contain this point.

columnMarginChanged

public void columnMarginChanged(ChangeEvent event)
Specified by:
columnMarginChanged in interface TableColumnModelListener

columnMoved

public void columnMoved(TableColumnModelEvent event)
Specified by:
columnMoved in interface TableColumnModelListener

columnRemoved

public void columnRemoved(TableColumnModelEvent event)
Specified by:
columnRemoved in interface TableColumnModelListener

columnSelectionChanged

public void columnSelectionChanged(ListSelectionEvent event)
Specified by:
columnSelectionChanged in interface TableColumnModelListener

configureEnclosingScrollPane

protected void configureEnclosingScrollPane()

convertColumnIndexToModel

public int convertColumnIndexToModel(int vc)

convertColumnIndexToView

public int convertColumnIndexToView(int mc)

createDefaultColumnModel

protected TableColumnModel createDefaultColumnModel()

createDefaultColumnsFromModel

public 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.

createDefaultDataModel

protected TableModel createDefaultDataModel()

createDefaultEditors

protected void createDefaultEditors()

createDefaultRenderers

protected void createDefaultRenderers()

createDefaultSelectionModel

protected ListSelectionModel createDefaultSelectionModel()

createDefaultTableHeader

protected JTableHeader createDefaultTableHeader()

createScrollPaneForTable

public static JScrollPane createScrollPaneForTable(JTable table)

Deprecated. 1.0.2, replaced by new JScrollPane(JTable)


doLayout

public void doLayout()
Layout the components in this container.
Overrides:
doLayout in interface Container

editCellAt

public boolean editCellAt(int row,
                          int column)
Programmatically starts editing the specified cell.
Parameters:
row - the row of the cell to edit.
column - the column of the cell to edit.

editCellAt

public boolean editCellAt(int row,
                          int column,
                          EventObject e)
Programmatically starts editing the specified cell.
Parameters:
row - the row of the cell to edit.
column - the column of the cell to edit.

editingCanceled

public void editingCanceled(ChangeEvent event)
Specified by:
editingCanceled in interface CellEditorListener

editingStopped

public void editingStopped(ChangeEvent event)
Specified by:
editingStopped in interface CellEditorListener

getAccessibleContext

public AccessibleContext getAccessibleContext()
Get the value of the JComponent.accessibleContext property.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface JComponent
Returns:
The current value of the property

getAutoCreateColumnsFromModel

public boolean getAutoCreateColumnsFromModel()
Get the value of the autoCreateColumnsFromModel property.
Returns:
The current value of the property

getAutoResizeMode

public int getAutoResizeMode()
Get the value of the autoResizeMode property.
Returns:
The current value of the property

getCellEditor

public TableCellEditor getCellEditor()
Get the value of the cellEditor property.
Returns:
The current value of the property

getCellEditor

public TableCellEditor getCellEditor(int row,
                                     int column)

getCellRect

public Rectangle getCellRect(int row,
                             int column,
                             boolean includeSpacing)
Calculate the visible rectangle for a particular row and column. The row and column are specified in visual terms; the column may not match the dataModel column.
Parameters:
row - the visible row to get the cell rectangle of
column - the visible column to get the cell rectangle of, which may differ from the dataModel column
includeSpacing - whether or not to include the cell margins in the resulting cell. If false, the result will only contain the inner area of the target cell, not including its margins.
Returns:
a rectangle enclosing the specified cell

getCellRenderer

public TableCellRenderer getCellRenderer(int row,
                                         int column)

getCellSelectionEnabled

public boolean getCellSelectionEnabled()
Get the value of the cellSelectionEnabled property.
Returns:
The current value of the property

getColumn

public TableColumn getColumn(Object identifier)

getColumnClass

public Class getColumnClass(int column)

getColumnCount

public int getColumnCount()
Get the value of the columnCount property by delegation to the @{link #columnModel} field.
Returns:
The current value of the columnCount property

getColumnModel

public TableColumnModel getColumnModel()
Get the value of the columnModel property.
Returns:
The current value of the property

getColumnName

public String getColumnName(int column)

getColumnSelectionAllowed

public boolean getColumnSelectionAllowed()
Get the value of the columnSelectionAllowed property.
Returns:
The current value of the columnSelectionAllowed property

getDefaultEditor

public TableCellEditor getDefaultEditor(Class columnClass)

getDefaultRenderer

public TableCellRenderer getDefaultRenderer(Class columnClass)

getDragEnabled

public boolean getDragEnabled()
Get the value of the dragEnabled property.
Returns:
The current value of the property

getEditingColumn

public int getEditingColumn()

getEditingRow

public int getEditingRow()

getEditorComponent

public Component getEditorComponent()

getGridColor

public Color getGridColor()
Get the value of the gridColor property.
Returns:
The current value of the property

getIntercellSpacing

public Dimension getIntercellSpacing()
Get the value of the intercellSpacing property.
Returns:
The current value of the property

getModel

public TableModel getModel()
Get the value of the dataModel property.
Returns:
The current value of the property

getPreferredScrollableViewportSize

public Dimension getPreferredScrollableViewportSize()
Get the value of the preferredViewportSize property.
Specified by:
getPreferredScrollableViewportSize in interface Scrollable
Returns:
The current value of the property

getRowCount

public int getRowCount()
Get the value of the rowCount property by delegation to the @{link #dataModel} field.
Returns:
The current value of the rowCount property

getRowHeight

public int getRowHeight()
Get the value of the rowHeight property.
Returns:
The current value of the property

getRowHeight

public int getRowHeight(int row)
Get the height of the specified row.
Parameters:
row - the row whose height to return

getRowMargin

public int getRowMargin()
Get the value of the rowMargin property.
Returns:
The current value of the property

getRowSelectionAllowed

public boolean getRowSelectionAllowed()
Get the value of the rowSelectionAllowed property.
Returns:
The current value of the property

getScrollableBlockIncrement

public int getScrollableBlockIncrement(Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Specified by:
getScrollableBlockIncrement in interface Scrollable

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Get the value of the scrollableTracksViewportHeight property.
Specified by:
getScrollableTracksViewportHeight in interface Scrollable
Returns:
The constant value false

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Get the value of the scrollableTracksViewportWidth property.
Specified by:
getScrollableTracksViewportWidth in interface Scrollable
Returns:
true unless the autoResizeMode property is AUTO_RESIZE_OFF

getScrollableUnitIncrement

public int getScrollableUnitIncrement(Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Specified by:
getScrollableUnitIncrement in interface Scrollable

getSelectedColumn

public int getSelectedColumn()
Get the value of the selectedColumn property by delegation to the @{link #columnModel} field.
Returns:
The current value of the selectedColumn property

getSelectedColumnCount

public int getSelectedColumnCount()
Get the value of the selectedColumnCount property by delegation to the @{link #columnModel} field.
Returns:
The current value of the selectedColumnCount property

getSelectedColumns

public int[] getSelectedColumns()
Get the value of the selectedColumns property by delegation to the @{link #columnModel} field.
Returns:
The current value of the selectedColumns property

getSelectedRow

public int getSelectedRow()
Get the value of the selectedRow property by delegation to the ListSelectionModel.getMinSelectionIndex() method of the selectionModel field.
Returns:
The current value of the selectedRow property

getSelectedRowCount

public int getSelectedRowCount()
Get the value of the selectedRowCount property by delegation to the @{link #selectionModel} field.
Returns:
The current value of the selectedRowCount property

getSelectedRows

public int[] getSelectedRows()
Get the value of the selectedRows property by delegation to the @{link #selectionModel} field.
Returns:
The current value of the selectedRows property

getSelectionBackground

public Color getSelectionBackground()
Get the value of the selectionBackground property.
Returns:
The current value of the property

getSelectionForeground

public Color getSelectionForeground()
Get the value of the selectionForeground property.
Returns:
The current value of the property

getSelectionModel

public ListSelectionModel getSelectionModel()
Get the value of the selectionModel property.
Returns:
The current value of the property

getShowHorizontalLines

public boolean getShowHorizontalLines()
Get the value of the showHorizontalLines property.
Returns:
The current value of the property

getShowVerticalLines

public boolean getShowVerticalLines()
Get the value of the showVerticalLines property.
Returns:
The current value of the property

getSurrendersFocusOnKeystroke

public boolean getSurrendersFocusOnKeystroke()
Returns whether cell editors of this table should receive keyboard focus when the editor is activated by a keystroke. The default setting is false which means that the table should keep the keyboard focus until the cell is selected by a mouse click.
Returns:
whether cell editors of this table should receive keyboard focus when the editor is activated by a keystroke
Since:
1.4

getTableHeader

public JTableHeader getTableHeader()
Get the value of the tableHeader property.
Returns:
The current value of the property

getUI

public TableUI getUI()
This method returns the table's UI delegate.
Returns:
The table's UI delegate.

getUIClassID

public String getUIClassID()
Overrides:
getUIClassID in interface JComponent

getValueAt

public Object getValueAt(int row,
                         int column)

initializeLocalVars

protected void initializeLocalVars()

isCellEditable

public boolean isCellEditable(int row,
                              int column)
Returns true if the specified cell is editable, and false otherwise.
Parameters:
row - the row index.
column - the column index.
Returns:
A boolean.

isCellSelected

public boolean isCellSelected(int row,
                              int column)

isColumnSelected

public boolean isColumnSelected(int column)

isEditing

public boolean isEditing()

isRowSelected

public boolean isRowSelected(int row)

moveColumn

public void moveColumn(int column,
                       int targetColumn)
Moves column at the specified index to new given location.
Parameters:
column - index of the column to move
targetColumn - index specifying new location of the column

prepareEditor

public 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).
Parameters:
editor - the TableCellEditor to set up
row - the row of the cell to edit
column - the column of the cell to edit
Returns:
the Component being edited

prepareRenderer

public Component prepareRenderer(TableCellRenderer renderer,
                                 int row,
                                 int column)

removeColumn

public void removeColumn(TableColumn column)
Removes specified column from displayable columns of this table.
Parameters:
column - column to removed

removeColumnSelectionInterval

public void removeColumnSelectionInterval(int index0,
                                          int index1)

removeEditor

public void removeEditor()
Discards the editor object.

removeNotify

public void removeNotify()
Called when this container is removed from its parent container to inform it to destroy its peer. This causes the peers of all child component to be destroyed as well.
Overrides:
removeNotify in interface JComponent

removeRowSelectionInterval

public void removeRowSelectionInterval(int index0,
                                       int index1)

resizeAndRepaint

protected void resizeAndRepaint()
This revalidates the JTable and queues a repaint.

rowAtPoint

public int rowAtPoint(Point point)
Returns index of the row that contains specified point or -1 if this table doesn't contain this point.
Parameters:
point - point to identify the row
Returns:
index of the row that contains specified point or -1 if this table doesn't contain this point.

selectAll

public void selectAll()

setAutoCreateColumnsFromModel

public void setAutoCreateColumnsFromModel(boolean autoCreate)
Set the value of the autoCreateColumnsFromModel flag. If the flag changes from false to true, the createDefaultColumnsFromModel() method is called.
Parameters:
autoCreate - the new value of the flag.

setAutoResizeMode

public void setAutoResizeMode(int a)
Set the value of the autoResizeMode property.
Parameters:
a - The new value of the autoResizeMode property

setCellEditor

public 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.

Parameters:
c - The new value of the cellEditor property

setCellSelectionEnabled

public void setCellSelectionEnabled(boolean c)
Set the value of the cellSelectionEnabled property.
Parameters:
c - The new value of the cellSelectionEnabled property

setColumnModel

public 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.

Parameters:
c - The new value of the columnModel property

setColumnSelectionAllowed

public void setColumnSelectionAllowed(boolean c)
Set the value of the columnSelectionAllowed property.
Parameters:
c - The new value of the property

setColumnSelectionInterval

public void setColumnSelectionInterval(int index0,
                                       int index1)

setDefaultEditor

public void setDefaultEditor(Class columnClass,
                             TableCellEditor editor)

setDefaultRenderer

public void setDefaultRenderer(Class columnClass,
                               TableCellRenderer rend)

setDragEnabled

public void setDragEnabled(boolean d)
Set the value of the dragEnabled property.
Parameters:
d - The new value of the dragEnabled property

setEditingColumn

public void setEditingColumn(int column)

setEditingRow

public void setEditingRow(int column)

setGridColor

public void setGridColor(Color g)
Set the value of the gridColor property.
Parameters:
g - The new value of the gridColor property

setIntercellSpacing

public void setIntercellSpacing(Dimension i)
Set the value of the intercellSpacing property.
Parameters:
i - The new value of the intercellSpacing property

setModel

public 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.

Parameters:
m - The new value of the model property

setPreferredScrollableViewportSize

public void setPreferredScrollableViewportSize(Dimension p)
Set the value of the preferredViewportSize property.
Parameters:
p - The new value of the preferredViewportSize property

setRowHeight

public void setRowHeight(int r)
Set the value of the rowHeight property.
Parameters:
r - The new value of the rowHeight property

setRowHeight

public void setRowHeight(int row,
                         int rh)
Sets the value of the rowHeight property for the specified row.
Parameters:
row - is the row to change the rowHeight of
rh - is the new rowHeight

setRowMargin

public void setRowMargin(int r)
Set the value of the rowMargin property.
Parameters:
r - The new value of the rowMargin property

setRowSelectionAllowed

public void setRowSelectionAllowed(boolean r)
Set the value of the rowSelectionAllowed property.
Parameters:
r - The new value of the rowSelectionAllowed property

setRowSelectionInterval

public void setRowSelectionInterval(int index0,
                                    int index1)

setSelectionBackground

public 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.

Parameters:
s - The new value of the selectionBackground property

setSelectionForeground

public 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.

Parameters:
s - The new value of the selectionForeground property

setSelectionMode

public void setSelectionMode(int s)
Set the value of the selectionMode property by delegation to the selectionModel field. The same selection mode is set for row and column selection models.
Parameters:
s - The new value of the property

setSelectionModel

public 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.

Parameters:
s - The new value of the selectionModel property

setShowGrid

public void setShowGrid(boolean s)
Set the value of the showGrid property.
Parameters:
s - The new value of the showGrid property

setShowHorizontalLines

public void setShowHorizontalLines(boolean s)
Set the value of the showHorizontalLines property.
Parameters:
s - The new value of the showHorizontalLines property

setShowVerticalLines

public void setShowVerticalLines(boolean s)
Set the value of the showVerticalLines property.
Parameters:
s - The new value of the showVerticalLines property

setSurrendersFocusOnKeystroke

public void setSurrendersFocusOnKeystroke(boolean value)
Sets whether cell editors of this table should receive keyboard focus when the editor is activated by a keystroke. The default setting is false which means that the table should keep the keyboard focus until the cell is selected by a mouse click.
Parameters:
value - the value to set
Since:
1.4

setTableHeader

public void setTableHeader(JTableHeader t)
Set the value of the tableHeader property.
Parameters:
t - The new value of the tableHeader property

setUI

public void setUI(TableUI ui)
This method sets the table's UI delegate.
Parameters:
ui - The table's UI delegate.

setValueAt

public void setValueAt(Object value,
                       int row,
                       int column)

sizeColumnsToFit

public void sizeColumnsToFit(boolean lastColumnOnly)

Deprecated. Replaced by doLayout()


sizeColumnsToFit

public void sizeColumnsToFit(int resizingColumn)
Obsolete since JDK 1.4. Please use doLayout().

tableChanged

public void tableChanged(TableModelEvent event)
Specified by:
tableChanged in interface TableModelListener

unconfigureEnclosingScrollPane

protected void unconfigureEnclosingScrollPane()

updateUI

public void updateUI()
Overrides:
updateUI in interface JComponent

valueChanged

public void valueChanged(ListSelectionEvent event)
Specified by:
valueChanged in interface ListSelectionListener

JTable.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.