APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JTable

extends JComponent
implements TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible, RowSorterListener
Class Inheritance
All Implemented Interfaces
javax.swing.event.RowSorterListener, java.util.EventListener, javax.accessibility.Accessible, javax.swing.event.CellEditorListener, javax.swing.event.ListSelectionListener, javax.swing.event.TableColumnModelListener, javax.swing.Scrollable, javax.swing.event.TableModelListener
Annotations
@JavaBean
defaultProperty:UI
description:A component which displays data in a two dimensional grid.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.Color, .Component, .Container, .Cursor, .Dimension, .Font, .FontMetrics, .Graphics, .GraphicsEnvironment, .HeadlessException, .IllegalComponentStateException, .KeyboardFocusManager, .Point, .Rectangle, .Window, java.awt.event.FocusListener, .KeyEvent, .MouseEvent, java.awt.print.PageFormat, .Printable, .PrinterAbortException, .PrinterException, .PrinterJob, java.beans.BeanProperty, .JavaBean, .PropertyChangeEvent, .PropertyChangeListener, java.io.IOException, .InvalidObjectException, .ObjectInputStream, .ObjectOutputStream, .Serial, java.text.DateFormat, .MessageFormat, .NumberFormat, java.util.Date, .Enumeration, .EventObject, .Hashtable, .Locale, .Vector, javax.accessibility.Accessible, .AccessibleAction, .AccessibleComponent, .AccessibleContext, .AccessibleExtendedTable, .AccessibleRole, .AccessibleSelection, .AccessibleState, .AccessibleStateSet, .AccessibleTable, .AccessibleTableModelChange, .AccessibleText, .AccessibleValue, javax.print.PrintService, javax.print.attribute.HashPrintRequestAttributeSet, .PrintRequestAttributeSet, javax.swing.border.Border, .EmptyBorder, .LineBorder, javax.swing.event.CellEditorListener, .ChangeEvent, .ListSelectionEvent, .ListSelectionListener, .RowSorterEvent, .RowSorterListener, .TableColumnModelEvent, .TableColumnModelListener, .TableModelEvent, .TableModelListener, javax.swing.plaf.TableUI, .UIResource, javax.swing.table.AbstractTableModel, .DefaultTableCellRenderer, .DefaultTableColumnModel, .DefaultTableModel, .JTableHeader, .TableCellEditor, .TableCellRenderer, .TableColumn, .TableColumnModel, .TableModel, .TableRowSorter, sun.awt.AWTAccessor, .AWTAccessor.MouseEventAccessor, sun.swing.PrintingStatus, .SwingUtilities2, .SwingUtilities2.Section

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 Integer.valueOf(row*col); }
     };
     JTable table = new JTable(dataModel);
     JScrollPane scrollpane = new JScrollPane(table);

JTables are typically placed inside of a JScrollPane. By default, a JTable will adjust its width such that a horizontal scrollbar is unnecessary. To allow for a horizontal scrollbar, invoke setAutoResizeMode with AUTO_RESIZE_OFF. 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.

To enable sorting and filtering of rows, use a RowSorter. You can set up a row sorter in either of two ways:

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. It is important to remember that the column and row indexes returned by various JTable methods are in terms of the JTable (the view) and are not necessarily the same indexes used by the model.

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.

Similarly when using the sorting and filtering functionality provided by RowSorter the underlying TableModel does not need to know how to do sorting, rather RowSorter will handle it. Coordinate conversions will be necessary when using the row based methods of JTable with the underlying TableModel. All of JTables row based methods are in terms of the RowSorter, which is not necessarily the same as that of the underlying TableModel. For example, the selection is always in terms of JTable so that when using RowSorter you will need to convert using convertRowIndexToView or convertRowIndexToModel. The following shows how to convert coordinates from JTable to that of the underlying model:

  int[] selection = table.getSelectedRows();
  for (int i = 0; i < selection.length; i++) {
    selection[i] = table.convertRowIndexToModel(selection[i]);
  }
  // selection is now in terms of the underlying TableModel

By default if sorting is enabled JTable will persist the selection and variable row heights in terms of the model on sorting. For example if row 0, in terms of the underlying model, is currently selected, after the sort row 0, in terms of the underlying model will be selected. Visually the selection may change, but in terms of the underlying model it will remain the same. The one exception to that is if the model index is no longer visible or was removed. For example, if row 0 in terms of model was filtered out the selection will be empty after the sort.

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

Swing is not thread safe. For more information see Swing's Threading Policy.

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 JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Authors
Philip Milne, Shannon Hickey (printing support)
Since
1.2
See Also
javax.swing.table.DefaultTableModel, javax.swing.table.TableRowSorter

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JTable.AccessibleJTable

This class implements accessibility support for the JTable class.

pack-priv static class
pack-priv static class
pack-priv class
pack-priv static class
pack-priv static class
public static class
JTable.DropLocation

A subclass of TransferHandler.DropLocation representing a drop location for a JTable.

pack-priv static class
JTable.GenericEditor

Default Editors

pack-priv static class
private class
JTable.ModelChange

ModelChange is used when sorting to restore state, it corresponds to data from a TableModelEvent.

pack-priv static class
pack-priv static class
JTable.NumberRenderer

Default Renderers

public static enum
JTable.PrintMode

Printing modes, used in printing JTables.

private static interface
private static interface
private class
JTable.SortManager

SortManager provides support for managing the selection and variable row heights when sorting is enabled.

private static class
JTable.ThreadSafePrintable

A Printable implementation that wraps another Printable, making it safe for printing on another thread.

Field Summary

Modifier and TypeField and Description
public static final int
AUTO_RESIZE_ALL_COLUMNS

During all resize operations, proportionately resize all columns.

public static final int
AUTO_RESIZE_LAST_COLUMN

During all resize operations, apply adjustments to the last column only.

public static final int
AUTO_RESIZE_NEXT_COLUMN

When a column is adjusted in the UI, adjust the next column the opposite way.

public static final int
AUTO_RESIZE_OFF

Do not adjust column widths automatically; use a horizontal scrollbar instead.

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

private boolean
autoCreateRowSorter

If true, any time the model changes a new RowSorter is set.

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 transient TableCellEditor
cellEditor

The active cell editor 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.

private boolean
columnSelectionAdjusting

The last value of getValueIsAdjusting from the column selection models columnSelectionChanged notification.

protected TableModel
dataModel

The TableModel of the table.

protected transient Hashtable<Object, Object>
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 transient Hashtable<Object, Object>
defaultRenderersByColumnClass

A table of objects that display the contents of a cell, indexed by class as declared in getColumnClass in the TableModel interface.

private boolean
private transient JTable.DropLocation
dropLocation

The drop location.

private DropMode
dropMode

The drop mode for this component.

protected transient int
editingColumn

Identifies the column of the cell being edited.

protected transient int
editingRow

Identifies the row of the cell being edited.

protected transient Component
editorComp

If editing, the Component that is handling the editing.

private PropertyChangeListener
private boolean
fillsViewportHeight

Whether or not the table always fills the viewport height.

protected Color
gridColor

The color of the grid.

private boolean
ignoreSortChange

If true, when sorterChanged is invoked it's value is ignored.

private boolean
isRowHeightSet

True when setRowHeight(int) has been invoked.

protected Dimension
preferredViewportSize

Used by the Scrollable interface to determine the initial visible area.

private Throwable
printError

To communicate errors between threads during printing.

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.

private SizeSequence
private boolean
rowSelectionAdjusting

The last value of getValueIsAdjusting from the row selection models valueChanged notification.

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.

private boolean
sorterChanged

Whether or not sorterChanged has been invoked.

private transient JTable.SortManager
sortManager

Information used in sorting.

private boolean
protected JTableHeader
tableHeader

The TableHeader working with the table.

private static final String
private transient boolean
updateInProgress

Flag to indicate UI update is in progress

private boolean
updateSelectionOnSort

If true, on a sort the selection is reset.

Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JTable()

Constructs a default JTable that is initialized with a default data model, a default column model, and a default selection model.

public
JTable(TableModel
the data model for the table
dm
)

Constructs a JTable that is initialized with dm as the data model, a default column model, and a default selection model.

public
JTable(TableModel
the data model for the table
dm
,
TableColumnModel
the column model for the table
cm
)

Constructs a JTable that is initialized with dm as the data model, cm as the column model, and a default selection model.

public
JTable(TableModel
the data model for the table
dm
,
TableColumnModel
the column model for the table
cm
,
ListSelectionModel
the row selection model for the table
sm
)

Constructs a JTable that is initialized with dm as the data model, cm as the column model, and sm as the selection model.

public
JTable(int
the number of rows the table holds
numRows
,
int
the number of columns the table holds
numColumns
)

Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel.

public
JTable(Vector<? extends Vector<E>>
the data for the new table
rowData
,
Vector<?>
names of each column
columnNames
)

Constructs a JTable to display the values in the Vector of Vectors, rowData, with column names, columnNames.

public
JTable(final Object[][]
the data for the new table
rowData
,
final Object[]
names of each column
columnNames
)

Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames.

Method Summary

Modifier and TypeMethod and Description
private void
accommodateDelta(int resizingColumnIndex, int delta)

public void
addColumn(TableColumn
the TableColumn to be added
aColumn
)

Appends aColumn to the end of the array of columns held by this JTable's column model.

public void
addColumnSelectionInterval(int
one end of the interval
index0
,
int
the other end of the interval
index1
)

Adds the columns from index0 to index1, inclusive, to the current selection.

public void
addNotify()

Overrides javax.swing.JComponent.addNotify.

Calls the configureEnclosingScrollPane method.

public void
addRowSelectionInterval(int
one end of the interval
index0
,
int
the other end of the interval
index1
)

Adds the rows from index0 to index1, inclusive, to the current selection.

private void
adjustSizes(long target, final JTable.Resizable3 r, boolean inverse)

private void
adjustSizes(long target, JTable.Resizable2 r, boolean limitToRange)

private int
boundColumn(int col)

private int
boundRow(int row)

public void
changeSelection(int
affects the selection at row
rowIndex
,
int
affects the selection at column
columnIndex
,
boolean
see description above
toggle
,
boolean
if true, extend the current selection
extend
)

Updates the selection models of the table, depending on the state of the two flags: toggle and extend.

private void
changeSelectionModel(ListSelectionModel sm, int index, boolean toggle, boolean extend, boolean selected, int anchor, boolean anchorSelected)

private void
checkDragEnabled(boolean b)

private static void
public void
clearSelection()

Deselects all selected columns and rows.

private void
public void
columnAdded(TableColumnModelEvent e)

Implements javax.swing.event.TableColumnModelListener.columnAdded.

Invoked when a column is added to the table column model.

public int

Returns:

the index of the column that point lies in, or -1 if the result is not in the range [0, getColumnCount()-1]
columnAtPoint
(Point
the location of interest
point
)

Returns the index of the column that point lies in, or -1 if the result is not in the range [0, getColumnCount()-1].

public void
columnMarginChanged(ChangeEvent
the event received
e
)

Implements javax.swing.event.TableColumnModelListener.columnMarginChanged.

Invoked when a column is moved due to a margin change.

public void
columnMoved(TableColumnModelEvent
the event received
e
)

Implements javax.swing.event.TableColumnModelListener.columnMoved.

Invoked when a column is repositioned.

public void
columnRemoved(TableColumnModelEvent e)

Implements javax.swing.event.TableColumnModelListener.columnRemoved.

Invoked when a column is removed from the table column model.

public void
columnSelectionChanged(ListSelectionEvent
the event received
e
)

Implements javax.swing.event.TableColumnModelListener.columnSelectionChanged.

Invoked when the selection model of the TableColumnModel is changed.

pack-priv void
compWriteObjectNotify()

Overrides javax.swing.JComponent.compWriteObjectNotify.

This is called from Component by way of reflection.

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.

private void
configureEnclosingScrollPaneUI()

This is a sub-part of configureEnclosingScrollPane() that configures anything on the scrollpane that may change when the look and feel changes.

public int

Returns:

the index of the corresponding column in the model
convertColumnIndexToModel
(int
the index of the column in the view
viewColumnIndex
)

Maps the index of the column in the view at viewColumnIndex to the index of the column in the table model.

public int

Returns:

the index of the corresponding column in the view
convertColumnIndexToView
(int
the index of the column in the model
modelColumnIndex
)

Maps the index of the column in the table model at modelColumnIndex to the index of the column in the view.

public int

Returns:

the index of the corresponding row in the model
convertRowIndexToModel
(int
the index of the row in the view
viewRowIndex
)

Maps the index of the row in terms of the view to the underlying TableModel.

private int
public int

Returns:

the index of the corresponding row in the view, or -1 if the row isn't visible
convertRowIndexToView
(int
the index of the row in terms of the model
modelRowIndex
)

Maps the index of the row in terms of the TableModel to the view.

private int
convertRowIndexToView(int modelIndex, JTable.ModelChange
describes the TableModelEvent that initiated the change; will be null if called as the result of a sort
change
)

Converts a model index to view index.

private int[]
convertSelectionToModel(RowSorterEvent e)

Converts the selection to model coordinates.

protected TableColumnModel

Returns:

the default column model object
createDefaultColumnModel
()

Returns the default column model object, which is a DefaultTableColumnModel.

public void
createDefaultColumnsFromModel()

Creates default columns for the table from the data model using the getColumnCount method defined in the TableModel interface.

protected TableModel

Returns:

the default table model object
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

Returns:

the default selection model object
createDefaultSelectionModel
()

Returns the default selection model object, which is a DefaultListSelectionModel.

protected JTableHeader

Returns:

the default table header object
createDefaultTableHeader
()

Returns the default table header object, which is a JTableHeader.

public static JScrollPane

Returns:

a JScrollPane created using aTable
createScrollPaneForTable
(JTable
a JTable to be used for the scroll pane
aTable
)
Deprecated As of Swing version 1.0.2, replaced by new JScrollPane(aTable).

Equivalent to new JScrollPane(aTable).

public void
doLayout()

Overrides java.awt.Container.doLayout.

Causes this table to lay out its rows and columns.

pack-priv JTable.DropLocation

Returns:

the drop location, or null
dropLocationForPoint
(Point
the point to calculate a drop location for
p
)

Overrides javax.swing.JComponent.dropLocationForPoint.

Calculates a drop location in this component, representing where a drop at the given point should insert data.

public boolean

Returns:

false if for any reason the cell cannot be edited, or if the indices are invalid
editCellAt
(int
the row to be edited
row
,
int
the column to be edited
column
)

Programmatically starts editing the cell at row and column, if those indices are in the valid range, and the cell at those indices is editable.

public boolean

Returns:

false if for any reason the cell cannot be edited, or if the indices are invalid
editCellAt
(int
the row to be edited
row
,
int
the column to be edited
column
,
EventObject
event to pass into shouldSelectCell; note that as of Java 2 platform v1.2, the call to shouldSelectCell is no longer made
e
)

Programmatically starts editing the cell at row and column, if those indices are in the valid range, and the cell at those indices is editable.

public void
editingCanceled(ChangeEvent
the event received
e
)

Implements javax.swing.event.CellEditorListener.editingCanceled.

Invoked when editing is canceled.

public void
editingStopped(ChangeEvent
the event received
e
)

Implements javax.swing.event.CellEditorListener.editingStopped.

Invoked when editing is finished.

public AccessibleContext

Returns:

an AccessibleJTable that serves as the AccessibleContext of this JTable
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JTable.

private int
getAdjustedIndex(int index, boolean row)

public boolean

Returns:

the autoCreateColumnsFromModel of the table
getAutoCreateColumnsFromModel
()

Determines whether the table will create default columns from the model.

public boolean

Returns:

true if a RowSorter should be created when the model changes
getAutoCreateRowSorter
()

Returns true if whenever the model changes, a new RowSorter should be created and installed as the table's sorter; otherwise, returns false.

public int

Returns:

the autoResizeMode of the table
getAutoResizeMode
()

Returns the auto resize mode of the table.

public TableCellEditor

Returns:

the TableCellEditor that does the editing, or null if the table is not currently editing.
getCellEditor
()

Returns the active cell editor, which is null if the table is not currently editing.

public TableCellEditor

Returns:

the editor for this cell; if null return the default editor for this type of cell
getCellEditor
(int
the row of the cell to edit, where 0 is the first row
row
,
int
the column of the cell to edit, where 0 is the first column
column
)

Returns an appropriate editor for the cell specified by row and column.

public Rectangle

Returns:

the rectangle containing the cell at location row,column
getCellRect
(int
the row index where the desired cell is located
row
,
int
the column index where the desired cell is located in the display; this is not necessarily the same as the column index in the data model for the table; the convertColumnIndexToView(int) method may be used to convert a data model column index to a display column index
column
,
boolean
if false, return the true cell bounds - computed by subtracting the intercell spacing from the height and widths of the column and row models
includeSpacing
)

Returns a rectangle for the cell that lies at the intersection of row and column.

public TableCellRenderer

Returns:

the assigned renderer; if null returns the default renderer for this type of object
getCellRenderer
(int
the row of the cell to render, where 0 is the first row
row
,
int
the column of the cell to render, where 0 is the first column
column
)

Returns an appropriate renderer for the cell specified by this row and column.

public boolean

Returns:

true if both row and column selection models are enabled
getCellSelectionEnabled
()

Returns true if both row and column selection models are enabled.

public TableColumn

Returns:

the TableColumn object that matches the identifier
getColumn
(Object
the identifier object
identifier
)

Returns the TableColumn object for the column in the table whose identifier is equal to identifier, when compared using equals.

public Class<?>

Returns:

the type of the column at position column in the view where the first column is column 0
getColumnClass
(int
the column in the view being queried
column
)

Returns the type of the column appearing in the view at column position column.

public int

Returns:

the number of columns in the table
getColumnCount
()

Returns the number of columns in the column model.

public TableColumnModel

Returns:

the object that provides the column state of the table
getColumnModel
()

Returns the TableColumnModel that contains all column information of this table.

public String

Returns:

the name of the column at position column in the view where the first column is column 0
getColumnName
(int
the column in the view being queried
column
)

Returns the name of the column appearing in the view at column position column.

public boolean

Returns:

true if columns can be selected, otherwise false
getColumnSelectionAllowed
()

Returns true if columns can be selected.

public TableCellEditor

Returns:

the default cell editor to be used for this columnClass
getDefaultEditor
(Class<?>
return the default cell editor for this columnClass
columnClass
)

Returns the editor to be used when no editor has been set in a TableColumn.

public TableCellRenderer

Returns:

the renderer for this columnClass
getDefaultRenderer
(Class<?>
return the default cell renderer for this columnClass
columnClass
)

Returns the cell renderer to be used when no renderer has been set in a TableColumn.

public boolean

Returns:

the value of the dragEnabled property
getDragEnabled
()

Returns whether or not automatic drag handling is enabled.

public final JTable.DropLocation

Returns:

the drop location
getDropLocation
()

Returns the location that this component should visually indicate as the drop location during a DnD operation over the component, or null if no location is to currently be shown.

public final DropMode

Returns:

the drop mode for this component
getDropMode
()

Returns the drop mode for this component.

public int

Returns:

the index of the column that contains the cell currently being edited; returns -1 if nothing being edited
getEditingColumn
()

Returns the index of the column that contains the cell currently being edited.

public int

Returns:

the index of the row that contains the cell currently being edited; returns -1 if nothing being edited
getEditingRow
()

Returns the index of the row that contains the cell currently being edited.

public Component

Returns:

Component handling editing session
getEditorComponent
()

Returns the component that is handling the editing session.

public boolean

Returns:

whether or not this table is always made large enough to fill the height of an enclosing viewport
getFillsViewportHeight
()

Returns whether or not this table is always made large enough to fill the height of an enclosing viewport.

public Color

Returns:

the color used to draw grid lines
getGridColor
()

Returns the color used to draw grid lines.

public Dimension

Returns:

the horizontal and vertical spacing between cells
getIntercellSpacing
()

Returns the horizontal and vertical space between cells.

private int
getLeadingCol(Rectangle visibleRect)

private int
getLeadingRow(Rectangle visibleRect)

public TableModel

Returns:

the TableModel that provides the data displayed by this JTable
getModel
()

Returns the TableModel that provides the data displayed by this JTable.

private int
getNextBlockIncrement(Rectangle visibleRect, int orientation)

Called to get the block increment for downward scrolling in cases of horizontal scrolling, or for vertical scrolling of a table with variable row heights.

public Dimension

Returns:

a Dimension object containing the preferredSize of the JViewport which displays this table
getPreferredScrollableViewportSize
()

Implements javax.swing.Scrollable.getPreferredScrollableViewportSize.

Returns the preferred size of the viewport for this table.

private int
getPreviousBlockIncrement(Rectangle visibleRect, int orientation)

Called to get the block increment for upward scrolling in cases of horizontal scrolling, or for vertical scrolling of a table with variable row heights.

public Printable

Returns:

a Printable for printing this JTable
getPrintable
(JTable.PrintMode
the printing mode that the printable should use
printMode
,
MessageFormat
a MessageFormat specifying the text to be used in printing a header, or null for none
headerFormat
,
MessageFormat
a MessageFormat specifying the text to be used in printing a footer, or null for none
footerFormat
)

Return a Printable for use in printing this JTable.

private TableColumn
public int

Returns:

the number of rows shown in the JTable
getRowCount
()

Returns the number of rows that can be shown in the JTable, given unlimited space.

public int

Returns:

the height in pixels of a table row
getRowHeight
()

Returns the height of a table row, in pixels.

public int

Returns:

the height, in pixels, of the cells in the row
getRowHeight
(int
the row whose height is to be returned
row
)

Returns the height, in pixels, of the cells in row.

public int

Returns:

the number of pixels between cells in a row
getRowMargin
()

Gets the amount of empty space, in pixels, between cells.

private SizeSequence
public boolean

Returns:

true if rows can be selected, otherwise false
getRowSelectionAllowed
()

Returns true if rows can be selected.

public RowSorter<? extends TableModel>

Returns:

the object responsible for sorting
getRowSorter
()

Returns the object responsible for sorting.

public int

Returns:

visibleRect.height or visibleRect.width per the orientation
getScrollableBlockIncrement
(Rectangle visibleRect, int orientation, int direction)

Implements javax.swing.Scrollable.getScrollableBlockIncrement.

Returns visibleRect.height or visibleRect.width, depending on this table's orientation.

public boolean

Returns:

false unless getFillsViewportHeight is true and the table needs to be stretched to fill the viewport
getScrollableTracksViewportHeight
()

Implements javax.swing.Scrollable.getScrollableTracksViewportHeight.

Returns false to indicate that the height of the viewport does not determine the height of the table, unless getFillsViewportHeight is true and the preferred height of the table is smaller than the viewport's height.

public boolean

Returns:

false if autoResizeMode is set to AUTO_RESIZE_OFF, otherwise returns true
getScrollableTracksViewportWidth
()

Implements javax.swing.Scrollable.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.

public int

Returns:

the "unit" increment for scrolling in the specified direction
getScrollableUnitIncrement
(Rectangle
the view area visible within the viewport
visibleRect
,
int
either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
orientation
,
int
less than zero to scroll up/left, greater than zero for down/right
direction
)

Implements javax.swing.Scrollable.getScrollableUnitIncrement.

Returns the scroll increment (in pixels) that completely exposes one new row or column (depending on the orientation).

public int

Returns:

the index of the first selected column
getSelectedColumn
()

Returns the index of the first selected column, -1 if no column is selected.

public int

Returns:

the number of selected columns, 0 if no columns are selected
getSelectedColumnCount
()

Returns the number of selected columns.

public int[]

Returns:

an array of integers containing the indices of all selected columns, or an empty array if no column is selected
getSelectedColumns
()

Returns the indices of all selected columns.

public int

Returns:

the index of the first selected row
getSelectedRow
()

Returns the index of the first selected row, -1 if no row is selected.

public int

Returns:

the number of selected rows, 0 if no rows are selected
getSelectedRowCount
()

Returns the number of selected rows.

public int[]

Returns:

an array of integers containing the indices of all selected rows, or an empty array if no row is selected
getSelectedRows
()

Returns the indices of all selected rows.

public Color

Returns:

the Color used for the background of selected list items
getSelectionBackground
()

Returns the background color for selected cells.

public Color

Returns:

the Color object for the foreground property
getSelectionForeground
()

Returns the foreground color for selected cells.

public ListSelectionModel

Returns:

the object that provides row selection state, null if row selection is not allowed
getSelectionModel
()

Returns the ListSelectionModel that is used to maintain row selection state.

public boolean

Returns:

true if the table draws horizontal lines between cells, false if it doesn't
getShowHorizontalLines
()

Returns true if the table draws horizontal lines between cells, false if it doesn't.

public boolean

Returns:

true if the table draws vertical lines between cells, false if it doesn't
getShowVerticalLines
()

Returns true if the table draws vertical lines between cells, false if it doesn't.

public boolean

Returns:

true if the editor should get the focus when keystrokes cause the editor to be activated
getSurrendersFocusOnKeystroke
()

Returns true if the editor should get the focus when keystrokes cause the editor to be activated

public JTableHeader

Returns:

the tableHeader used by this table
getTableHeader
()

Returns the tableHeader used by this JTable.

public String
getToolTipText(MouseEvent event)

Overrides javax.swing.JComponent.getToolTipText.

Overrides JComponent's getToolTipText method in order to allow the renderer's tips to be used if it has text set.

private int
private int
public TableUI

Returns:

the TableUI object that renders this component
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

public String

Returns:

the string "TableUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns the suffix used to construct the name of the L&F class used to render this component.

public boolean

Returns:

whether to update the selection on a sort
getUpdateSelectionOnSort
()

Returns true if the selection should be updated after sorting.

public Object

Returns:

the Object at the specified cell
getValueAt
(int
the row whose value is to be queried
row
,
int
the column whose value is to be queried
column
)

Returns the cell value at row and column.

protected void
initializeLocalVars()

Initializes table properties to their default values.

public boolean

Returns:

true if the cell is editable
isCellEditable
(int
the row whose value is to be queried
row
,
int
the column whose value is to be queried
column
)

Returns true if the cell at row and column is editable.

public boolean

Returns:

true if row and column are valid indices and the cell at index (row, column) is selected, where the first row and first column are at index 0
isCellSelected
(int
the row being queried
row
,
int
the column being queried
column
)

Returns true if the specified indices are in the valid range of rows and columns and the cell at the specified position is selected.

public boolean

Returns:

true if column is a valid index and the column at that index is selected (where 0 is the first column)
isColumnSelected
(int
the column in the column model
column
)

Returns true if the specified index is in the valid range of columns, and the column at that index is selected.

public boolean

Returns:

true if the table is editing a cell
isEditing
()

Returns true if a cell is being edited.

public boolean

Returns:

true if row is a valid index and the row at that index is selected (where 0 is the first row)
isRowSelected
(int
a row in the row model
row
)

Returns true if the specified index is in the valid range of rows, and the row at that index is selected.

private static boolean
private int
leadingEdge(Rectangle rect, int orientation)

private int
limit(int i, int a, int b)

public void
moveColumn(int
the index of column to be moved
column
,
int
the new index of the column
targetColumn
)

Moves the column column to the position currently occupied by the column targetColumn in the view.

private void
notifySorter(JTable.ModelChange change)

Notifies the sorter of a change in the underlying model.

protected String

Returns:

a string representation of this table
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this table.

public Component

Returns:

the Component being edited
prepareEditor
(TableCellEditor
the TableCellEditor to set up
editor
,
int
the row of the cell to edit, where 0 is the first row
row
,
int
the column of the cell to edit, where 0 is the first column
column
)

Prepares the editor by querying the data model for the value and selection state of the cell at row, column.

public Component

Returns:

the Component under the event location
prepareRenderer
(TableCellRenderer
the TableCellRenderer to prepare
renderer
,
int
the row of the cell to render, where 0 is the first row
row
,
int
the column of the cell to render, where 0 is the first column
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)

Overrides javax.swing.JComponent.processKeyBinding.

Invoked to process the key bindings for ks as the result of the KeyEvent e.

private void
readObject(ObjectInputStream s)

Hides javax.swing.JComponent.readObject.

We use the ObjectInputStream "registerValidation" callback to update the UI for the entire tree of components after they've all been read in.

public void
removeColumn(TableColumn
the TableColumn to be removed
aColumn
)

Removes aColumn from this JTable's array of columns.

public void
removeColumnSelectionInterval(int
one end of the interval
index0
,
int
the other end of the interval
index1
)

Deselects the columns from index0 to index1, inclusive.

public void
removeEditor()

Discards the editor object and frees the real estate it used for cell rendering.

public void
removeNotify()

Overrides javax.swing.JComponent.removeNotify.

Calls the unconfigureEnclosingScrollPane method.

public void
removeRowSelectionInterval(int
one end of the interval
index0
,
int
the other end of the interval
index1
)

Deselects the rows from index0 to index1, inclusive.

private void
repaintSortedRows(JTable.ModelChange change)

Repaints the sort of sorted rows in response to a TableModelEvent.

protected void
resizeAndRepaint()

Equivalent to revalidate followed by repaint.

private void
restoreSortingEditingRow(int
new index of the editingRow, in terms of the view
editingRow
)

Restores the editing row after a model event/sort order change.

private void
restoreSortingSelection(int[] selection, int lead, JTable.ModelChange change)

Restores the selection after a model event/sort order changes.

public int

Returns:

the index of the row that point lies in, or -1 if the result is not in the range [0, getRowCount()-1]
rowAtPoint
(Point
the location of interest
point
)

Returns the index of the row that point lies in, or -1 if the result is not in the range [0, getRowCount()-1].

public void
selectAll()

Selects all rows, columns, and cells in the table.

private void
selectColumns(int columnCount)

private void
selectRows(int rowCount)

public void
setAutoCreateColumnsFromModel(boolean
true if JTable should automatically create columns
autoCreateColumnsFromModel
)

Sets this table's autoCreateColumnsFromModel flag.

public void
setAutoCreateRowSorter(boolean
whether or not a RowSorter should be automatically created
autoCreateRowSorter
)

Specifies whether a RowSorter should be created for the table whenever its model changes.

public void
setAutoResizeMode(int
One of 5 legal values: AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS, AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNS
mode
)

Sets the table's auto resize mode when the table is resized.

public void
setCellEditor(TableCellEditor
the active cell editor
anEditor
)

Sets the active cell editor.

public void
setCellSelectionEnabled(boolean
true if simultaneous row and column selection is allowed
cellSelectionEnabled
)

Sets whether this table allows both a column selection and a row selection to exist simultaneously.

public void
setColumnModel(final TableColumnModel
the new data source for this table
columnModel
)

Sets the column model for this table to columnModel and registers for listener notifications from the new column model.

public void
setColumnSelectionAllowed(boolean
true if this model will allow column selection
columnSelectionAllowed
)

Sets whether the columns in this model can be selected.

public void
setColumnSelectionInterval(int
one end of the interval
index0
,
int
the other end of the interval
index1
)

Selects the columns from index0 to index1, inclusive.

public void
setDefaultEditor(Class<?>
set the default cell editor for this columnClass
columnClass
,
TableCellEditor
default cell editor to be used for this columnClass
editor
)

Sets a default cell editor to be used if no editor has been set in a TableColumn.

public void
setDefaultRenderer(Class<?>
set the default cell renderer for this columnClass
columnClass
,
TableCellRenderer
default cell renderer to be used for this columnClass
renderer
)

Sets a default cell renderer to be used if no renderer has been set in a TableColumn.

public void
setDragEnabled(boolean
whether or not to enable automatic drag handling
b
)

Turns on or off automatic drag handling.

pack-priv Object

Returns:

any saved state for this component, or null if none
setDropLocation
(TransferHandler.DropLocation
the drop location (as calculated by dropLocationForPoint) or null if there's no longer a valid drop location
location
,
Object
the state object saved earlier for this component, or null
state
,
boolean
whether or not the method is being called because an actual drop occurred
forDrop
)

Overrides javax.swing.JComponent.setDropLocation.

Called to set or clear the drop location during a DnD operation.

public final void
setDropMode(DropMode
the drop mode to use
dropMode
)

Sets the drop mode for this component.

public void
setEditingColumn(int
the column of the cell to be edited
aColumn
)

Sets the editingColumn variable.

public void
setEditingRow(int
the row of the cell to be edited
aRow
)

Sets the editingRow variable.

public void
setFillsViewportHeight(boolean
whether or not this table is always made large enough to fill the height of an enclosing viewport
fillsViewportHeight
)

Sets whether or not this table is always made large enough to fill the height of an enclosing viewport.

public void
setGridColor(Color
the new color of the grid lines
gridColor
)

Sets the color used to draw grid lines to gridColor and redisplays.

public void
setIntercellSpacing(Dimension
a Dimension specifying the new width and height between cells
intercellSpacing
)

Sets the rowMargin and the columnMargin -- the height and width of the space between cells -- to intercellSpacing.

public void
setModel(final TableModel
the new data source for this table
dataModel
)

Sets the data model for this table to dataModel and registers with it for listener notifications from the new data model.

public void
setPreferredScrollableViewportSize(Dimension
a Dimension object specifying the preferredSize of a JViewport whose view is this table
size
)

Sets the preferred size of the viewport for this table.

public void
setRowHeight(int
new row height
rowHeight
)

Sets the height, in pixels, of all cells to rowHeight, revalidates, and repaints.

public void
setRowHeight(int
the row whose height is being changed
row
,
int
new row height, in pixels
rowHeight
)

Sets the height for row to rowHeight, revalidates, and repaints.

public void
setRowMargin(int
the number of pixels between cells in a row
rowMargin
)

Sets the amount of empty space between cells in adjacent rows.

public void
setRowSelectionAllowed(boolean
true if this model will allow row selection
rowSelectionAllowed
)

Sets whether the rows in this model can be selected.

public void
setRowSelectionInterval(int
one end of the interval
index0
,
int
the other end of the interval
index1
)

Selects the rows from index0 to index1, inclusive.

public void
setRowSorter(RowSorter<? extends TableModel>
the RowSorter; null turns sorting off
sorter
)

Sets the RowSorter.

public void
setSelectionBackground(Color
the Color to use for the background of selected cells
selectionBackground
)

Sets the background color for selected cells.

public void
setSelectionForeground(Color
the Color to use in the foreground for selected list items
selectionForeground
)

Sets the foreground color for selected cells.

public void
setSelectionMode(int
the mode used by the row and column selection models
selectionMode
)

Sets the table's selection mode to allow only single selections, a single contiguous interval, or multiple intervals.

public void
setSelectionModel(final ListSelectionModel
the new selection model
selectionModel
)

Sets the row selection model for this table to selectionModel and registers for listener notifications from the new selection model.

public void
setShowGrid(boolean
true if table view should draw grid lines
showGrid
)

Sets whether the table draws grid lines around cells.

public void
setShowHorizontalLines(boolean
true if table view should draw horizontal lines
showHorizontalLines
)

Sets whether the table draws horizontal lines between cells.

public void
setShowVerticalLines(boolean
true if table view should draw vertical lines
showVerticalLines
)

Sets whether the table draws vertical lines between cells.

public void
setSurrendersFocusOnKeystroke(boolean
true if the editor should get the focus when keystrokes cause the editor to be activated
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.

public void
setTableHeader(JTableHeader
new tableHeader
tableHeader
)

Sets the tableHeader working with this JTable to newHeader.

public void
setUI(TableUI
the TableUI L&F object
ui
)

Sets the L&F object that renders this component and repaints.

pack-priv void
public void
setUpdateSelectionOnSort(boolean
whether or not to update the selection on sorting
update
)

Specifies whether the selection should be updated after sorting.

public void
setValueAt(Object
the new value
aValue
,
int
the row of the cell to be changed
row
,
int
the column of the cell to be changed
column
)

Sets the value for the cell in the table model at row and column.

private void
setWidthsFromPreferredWidths(final boolean inverse)

public void
sizeColumnsToFit(boolean
determines whether to resize last column only
lastColumnOnly
)
Deprecated As of Swing version 1.0.3, replaced by doLayout().

Sizes the table columns to fit the available space.

public void
sizeColumnsToFit(int
the column whose resizing made this adjustment necessary or -1 if there is no such column
resizingColumn
)

Obsolete as of Java 2 platform v1.4.

private void
sortedTableChanged(RowSorterEvent sortedEvent, TableModelEvent e)

Invoked when sorterChanged is invoked, or when tableChanged is invoked and sorting is enabled.

public void
sorterChanged(RowSorterEvent
the RowSorterEvent describing the change
e
)

Implements javax.swing.event.RowSorterListener.sorterChanged.

RowSorterListener notification that the RowSorter has changed in some way.

public void
tableChanged(TableModelEvent e)

Implements javax.swing.event.TableModelListener.tableChanged.

Invoked when this table's TableModel generates a TableModelEvent.

private void
private void
private int
trailingEdge(Rectangle rect, int orientation)

protected void
unconfigureEnclosingScrollPane()

Reverses the effect of configureEnclosingScrollPane by replacing the columnHeaderView of the enclosing scroll pane with null.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Notification from the UIManager that the L&F has changed.

public void
valueChanged(ListSelectionEvent
the event received
e
)

Implements javax.swing.event.ListSelectionListener.valueChanged.

Invoked when the row selection changes -- repaints to show the new selection.

private int
private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcontainscreateToolTipdisabledndDoneenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate