The table component, displaying information, organized in rows and columns.
The table can be placed in the scroll bar and have the optional header
that is always visible. Cell values may be editable after double clicking
on the cell. Cell columns may have various data types, that are
displayed and edited by the different renderers and editors. It is possible
to set different column width. The columns are also resizeable by
dragging the column boundary in the header.
addColumn
public void addColumn(TableColumn column)
Add the new table column. The table column class allows to specify column
features more precisely, setting the preferred width, column data type
(column class) and table headers.
There is no need the add columns to the table if the default column
handling is sufficient.
column
- the new column to add.
changeSelection
public void changeSelection(int rowIndex,
int columnIndex,
boolean toggle,
boolean extend)
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.
point
- point to identify the column
- index of the column that contains specified point or
-1 if this table doesn't contain this point.
convertColumnIndexToModel
public int convertColumnIndexToModel(int vc)
Convert the table model index into the table column number.
The model number need not match the real column position. The columns
may be rearranged by the user with mouse at any time by dragging the
column headers.
vc
- the column number (0=first).
- the table column model index of this column.
convertColumnIndexToView
public int convertColumnIndexToView(int mc)
Convert the table column number to the table column model index.
The model number need not match the real column position. The columns
may be rearranged by the user with mouse at any time by dragging the
column headers.
mc
- the table column index (0=first).
- the table column number in the model
createDefaultEditors
protected void createDefaultEditors()
Create the default editors for this table. The default method creates
the editor for Booleans.
Other fields are edited as strings at the moment.
createDefaultRenderers
protected void createDefaultRenderers()
Create the default renderers for this table. The default method creates
renderers for Boolean, Number, Double, Date, Icon and ImageIcon.
createDefaultTableHeader
protected JTableHeader createDefaultTableHeader()
Create the default table header, if the user - defined table header is not
provided.
- the default table header.
doLayout
public void doLayout()
Set the widths of all columns, taking they preferred widths into
consideration. The excess space, if any, will be distrubuted between
all columns. This method also handles special cases when one of the
collumns is currently being resized.
- doLayout in interface Container
editCellAt
public boolean editCellAt(int row,
int column)
Programmatically starts editing the specified cell.
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.
row
- the row of the cell to edit.column
- the column of the cell to edit.
getCellEditor
public TableCellEditor getCellEditor(int row,
int column)
Get the cell editor, suitable for editing the given cell. The default
method requests the editor from the column model. If the column model does
not provide the editor, the call is forwarded to the
getDefaultEditor(Class)
with the parameter, obtained from
TableModel.getColumnClass(int)
.
row
- the cell rowcolumn
- the cell column
- the editor to edit that cell
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.
row
- the visible row to get the cell rectangle ofcolumn
- the visible column to get the cell rectangle of, which may
differ from the dataModel
columnincludeSpacing
- 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.
- a rectangle enclosing the specified cell
getCellRenderer
public TableCellRenderer getCellRenderer(int row,
int column)
Get the cell renderer for rendering the given cell.
row
- the cell rowcolumn
- the cell column
- the cell renderer to render that cell.
getColumn
public TableColumn getColumn(Object identifier)
Get table column with the given identified.
identifier
- the column identifier
- the table column with this identifier
getColumnClass
public Class getColumnClass(int column)
Get the class (datatype) of the column. The cells are rendered and edited
differently, depending from they data type.
column
- the column (not the model index).
- the class, defining data type of that column (String.class for
String, Boolean.class for boolean and so on).
getColumnCount
public int getColumnCount()
Get the value of the
columnCount
property by
delegation to the
columnModel
field.
- The current value of the columnCount property
getColumnName
public String getColumnName(int column)
Get the name of the column. If the column has the column identifier set,
the return value is the result of the .toString() method call on that
identifier. If the identifier is not explicitly set, the returned value
is calculated by
AbstractTableModel.getColumnName(int)
.
getColumnSelectionAllowed
public boolean getColumnSelectionAllowed()
Get the value of the columnSelectionAllowed
property.
- The current value of the columnSelectionAllowed property
getDefaultEditor
public TableCellEditor getDefaultEditor(Class columnClass)
Get the default editor for editing values of the given type
(String, Boolean and so on).
columnClass
- the class of the value that will be edited.
- the editor, suitable for editing this data type
getDefaultRenderer
public TableCellRenderer getDefaultRenderer(Class columnClass)
Get the default renderer for rendering the given data type.
columnClass
- the data that must be rendered
- the appropriate defauld renderer for rendering that data type.
getDragEnabled
public boolean getDragEnabled()
- The current value of the property
getEditingColumn
public int getEditingColumn()
Get the column, currently being edited
- the column, currently being edited.
getEditingRow
public int getEditingRow()
Get the row currently being edited.
- the row, currently being edited.
getEditorComponent
public Component getEditorComponent()
Get the editor component that is currently editing one of the cells
- the editor component or null, if none of the cells is being
edited.
getRowCount
public int getRowCount()
Get the value of the
rowCount
property by
delegation to the
dataModel
field.
- The current value of the rowCount property
getRowHeight
public int getRowHeight()
- The current value of the property
getRowHeight
public int getRowHeight(int row)
Get the height of the specified row.
row
- the row whose height to return
getRowMargin
public int getRowMargin()
- The current value of the property
getScrollableBlockIncrement
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction)
Return the preferred scrolling amount (in pixels) for the given
scrolling direction and orientation when scrolling in large amounts
(pages).
- getScrollableBlockIncrement in interface Scrollable
visibleRect
- the currently visible part of the component.orientation
- the scrolling orientationdirection
- the scrolling direction (negative - up, positive -down).
The values greater than one means that more mouse wheel or similar
events were generated, and hence it is better to scroll the longer
distance.
- the preferred scrolling distance, negative if up or left.
getScrollableUnitIncrement
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Return the preferred scrolling amount (in pixels) for the given scrolling
direction and orientation. This method handles a partially exposed row by
returning the distance required to completely expose the item. When
scrolling the top item is completely exposed.
- getScrollableUnitIncrement in interface Scrollable
visibleRect
- the currently visible part of the component.orientation
- the scrolling orientationdirection
- the scrolling direction (negative - up, positive -down).
The values greater than one means that more mouse wheel or similar
events were generated, and hence it is better to scroll the longer
distance.
getSelectedColumn
public int getSelectedColumn()
Get the value of the
selectedColumn
property by
delegation to the
columnModel
field.
- The current value of the selectedColumn property
getSelectedColumnCount
public int getSelectedColumnCount()
Get the value of the
selectedColumnCount
property by
delegation to the
columnModel
field.
- The current value of the selectedColumnCount property
getSelectedColumns
public int[] getSelectedColumns()
Get the value of the
selectedColumns
property by
delegation to the
columnModel
field.
- The current value of the selectedColumns property
getSelectedRow
public int getSelectedRow()
- The current value of the selectedRow property
getSelectedRowCount
public int getSelectedRowCount()
Get the value of the
selectedRowCount
property by
delegation to the
selectionModel
field.
- The current value of the selectedRowCount property
getSelectedRows
public int[] getSelectedRows()
Get the value of the
selectedRows
property by
delegation to the
selectionModel
field.
- The current value of the selectedRows 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.
- whether cell editors of this table should receive keyboard focus
when the editor is activated by a keystroke
getUI
public TableUI getUI()
This method returns the table's UI delegate.
getValueAt
public Object getValueAt(int row,
int column)
Get the cell value at the given position.
row
- the row to get the valuecolumn
- the actual column number (not the model index)
to get the value.
- the cell value, as returned by model.
initializeLocalVars
protected void initializeLocalVars()
Initialize local variables to default values.
isCellEditable
public boolean isCellEditable(int row,
int column)
Returns true
if the specified cell is editable, and
false
otherwise.
row
- the row index.column
- the column index.
- true if the cell is editable, false otherwise.
isCellSelected
public boolean isCellSelected(int row,
int column)
Checks if the given cell is selected. The cell is selected if both
the cell row and the cell column are selected.
row
- the cell rowcolumn
- the cell column
- true if the cell is selected, false otherwise
isColumnSelected
public boolean isColumnSelected(int column)
Checks if the given column is selected.
- true if the column is selected (as reported by the selection
model, associated with the column model), false otherwise.
isEditing
public boolean isEditing()
Check if one of the table cells is currently being edited.
- true if there is a cell being edited.
isRowSelected
public boolean isRowSelected(int row)
Checks if the given row is selected.
- true if the row is selected (as reported by the selection model),
false otherwise.
moveColumn
public void moveColumn(int column,
int targetColumn)
Moves column at the specified index to new given location.
column
- index of the column to movetargetColumn
- 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).
editor
- the TableCellEditor to set uprow
- the row of the cell to editcolumn
- the column of the cell to edit
- the Component being edited
prepareRenderer
public Component prepareRenderer(TableCellRenderer renderer,
int row,
int column)
Prepare the renderer for rendering the given cell.
renderer
- the renderer being preparedrow
- the row of the cell being renderedcolumn
- the column of the cell being rendered
- the component which .paint() method will paint the cell.
removeColumn
public void removeColumn(TableColumn column)
Removes specified column from displayable columns of this table.
column
- column to removed
removeEditor
public void removeEditor()
Discards the editor object.
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.
point
- point to identify the row
- index of the row that contains specified point or -1 if this table
doesn't contain this point.
selectAll
public void selectAll()
Select all table.
setAutoResizeMode
public void setAutoResizeMode(int a)
a
- The new value of the autoResizeMode property
setCellSelectionEnabled
public void setCellSelectionEnabled(boolean c)
c
- The new value of the cellSelectionEnabled property
setColumnSelectionAllowed
public void setColumnSelectionAllowed(boolean c)
Set the value of the columnSelectionAllowed
property.
c
- The new value of the property
setDefaultEditor
public void setDefaultEditor(Class columnClass,
TableCellEditor editor)
Set the default editor for the given column class (column data type).
By default, String is handled by text field and Boolean is handled by
the check box.
columnClass
- the column data typeeditor
- the editor that will edit this data type
setDefaultRenderer
public void setDefaultRenderer(Class columnClass,
TableCellRenderer rend)
Set default renderer for rendering the given data type.
columnClass
- the data type (String, Boolean and so on) that must be
rendered.rend
- the renderer that will rend this data type
setDragEnabled
public void setDragEnabled(boolean d)
d
- The new value of the dragEnabled property
setEditingColumn
public void setEditingColumn(int column)
Set the column, currently being edited
column
- the column, currently being edited.
setEditingRow
public void setEditingRow(int row)
Set the row currently being edited.
row
- the row, that will be edited
setGridColor
public void setGridColor(Color g)
g
- The new value of the gridColor property
setIntercellSpacing
public void setIntercellSpacing(Dimension i)
Set the value of the intercellSpacing
property.
i
- The new value of the intercellSpacing property
setRowHeight
public void setRowHeight(int r)
Sets the height for all rows in the table. If you want to change the
height of a single row instead, use
setRowHeight(int,int)
.
r
- the height to set for all rows
setRowHeight
public void setRowHeight(int row,
int rh)
Sets the height of a single row in the table.
row
- the row to change the height ofrh
- the new row height
setRowMargin
public void setRowMargin(int r)
r
- The new value of the rowMargin property
setRowSelectionAllowed
public void setRowSelectionAllowed(boolean r)
r
- The new value of the rowSelectionAllowed 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.
s
- The new value of the property
setShowGrid
public void setShowGrid(boolean s)
Set the value of the showGrid
property.
s
- The new value of the showGrid property
setShowHorizontalLines
public void setShowHorizontalLines(boolean s)
s
- The new value of the showHorizontalLines property
setShowVerticalLines
public void setShowVerticalLines(boolean s)
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.
setUI
public void setUI(TableUI ui)
This method sets the table's UI delegate.
ui
- The table's UI delegate.
setValueAt
public void setValueAt(Object value,
int row,
int column)
Set value for the cell at the given position. The modified cell is
repainted.
value
- the value to setrow
- the row of the cell being modifiedcolumn
- the column of the cell being modified
sizeColumnsToFit
public void sizeColumnsToFit(boolean lastColumnOnly)
Replaced by doLayout()
sizeColumnsToFit
public void sizeColumnsToFit(int resizingColumn)
Obsolete since JDK 1.4. Please use doLayout()
.
JTable.java --
Copyright (C) 2002, 2004, 2005, 2006 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.