class org.aswing.JTable extends Container
Interfaces | |||||||
LayoutManager, CellEditorListener, TableColumnModelListener, TableModelListener, Viewportable |
|
The JTable
is used to display and edit regular two-dimensional tables
of cells.
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:
class MyTableModel extends AbstractTableModel{ public function getColumnCount():int { return 10; } public function getRowCount():int { return 10;} public getValueAt(row:int, col:int) { return row*col; } }; var dataModel:MyTableModel = new MyTableModel(); var table:JTable = new JTable(dataModel); var scrollpane:JScrollPane = new JScrollPane(table);
Note that if you wish to use a JTable
in a standalone
view (outside of a JScrollPane
) and want the header
displayed, you can get it using {@link #getTableHeader} and
display it separately.
When designing applications that use the JTable
it is worth paying
close attention to the data structures that will represent the table's data.
The DefaultTableModel
is a model implementation that
uses a Vector
of Vector
s of Object
s 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 JTable
uses integers exclusively to refer to both the rows and the columns
of the model that it displays. The JTable
simply takes a tabular range of cells
and uses getValueAt(int, int)
to retrieve the
values from the model during painting.
By default, columns may be rearranged in the JTable
so that the
view's columns appear in a different order to the columns in the model.
This does not affect the implementation of the model at all: when the
columns are reordered, the JTable
maintains the new order of the columns
internally and converts its column indices before querying the model.
To listen table row selection change event, by this.addSelectionListener()
To listen column selection see org.aswing.table.TableColumnModel.addColumnModelListener
To listen other Table events see org.aswing.table.TableModel.addTableModelListener
Show Events | Hide Events
|
Class Fields
static var AUTO_RESIZE_ALL_COLUMNS:Int
During all resize operations, proportionately resize all columns.
static var AUTO_RESIZE_LAST_COLUMN:Int
During all resize operations, apply adjustments to the last column only.
static var AUTO_RESIZE_NEXT_COLUMN:Int
When a column is adjusted in the UI, adjust the next column the opposite way.
static var AUTO_RESIZE_SUBSEQUENT_COLUMNS:Int
During UI adjustment, change subsequent columns to preserve the total width; this is the default behavior.
Instance Fields
var autoResizeMode:Int
The table's auto resize mode when the table is resized.
One of 5 legal values:
this.AUTO_RESIZE_OFF
,
this.AUTO_RESIZE_NEXT_COLUMN
,
this.AUTO_RESIZE_SUBSEQUENT_COLUMNS
,
this.AUTO_RESIZE_LAST_COLUMN
,
this.AUTO_RESIZE_ALL_COLUMNS
The default mode is this.AUTO_RESIZE_SUBSEQUENT_COLUMNS
.
Whether this table allows both a column selection and a row selection to exist simultaneously.
When set, the table treats the intersection of the row and column selection models as the selected cells.
Override this.isCellSelected
to
change this default behavior.
This property is equivalent to setting
both the this.rowSelectionAllowed
property and
this.columnSelectionAllowed
property to the supplied value.
See Also
- this.isCellSelected()
var dataModel:TableModel
The data model for this table to newModel
and registers
with it for listener notifications from the new data model.
The height, in pixels, of all cells.
The height of the cells will be equal to the row height minus the row margin.
default value | 20 |
var selectedItem:Dynamic
It works only if dataModel is PropertyTableModel
or TableSorter
with PropertyTableModel
inside.
var selectionMode:Int
Sets the table's selection mode to allow only single selections, a single contiguous interval, or multiple intervals.
JTable
provides all the methods for handling
column and row selection. When setting states,
such as setSelectionMode
, it not only
updates the mode for the row selection model but also sets similar
values in the selection model of the columnModel
.
If you want to have the row and column selection models operating
in different modes, set them both directly.
Both the row and column selection models for JTable
default to using a DefaultListSelectionModel
so that JTable
works the same way as the
JList
. See the setSelectionMode
method
in JList
for details about the modes.
See Also
Whether the table draws horizontal lines between cells.
You may also need to use cellSpacing
to set a proper gap of
columns and rows to avoid lines effect caused by different bgs between table and cell.
See Also
- this.showGrid
- this.showVerticalLines
- this.cellSpacing
Whether the table draws vertical lines between cells.
If this.showVerticalLines
is true it does; if it is false it doesn't.
You may also need to call setIntercellSpacing() to set a proper gap of columns and rows to avoid lines effect caused by different bgs between table and cell.
See Also
- this.showHorizontalLines
- this.showIntercellSpacing
function addColumn(aColumn:TableColumn): Void
Appends aColumn
to the end of the array of columns held by
this JTable
's column model.
If the column name of aColumn
is null
,
sets the column name of aColumn
to the name
returned by getModel().getColumnName()
.
To add a column to this JTable
to display the
modelColumn
'th column of data in the model with a
given width
, cellRenderer
,
and cellEditor
you can use:
[Any of the `TableColumn` constructors can be used instead of this one.] The model column number is stored inside theaddColumn(new TableColumn(modelColumn, width, cellRenderer, cellEditor));
TableColumn
and is used during rendering and editing to locate the appropriates
data values in the model. The model column number does not change
when columns are reordered in the view.
@param aColumn the TableColumn
to be added
@see #removeColumn
function addColumnSelectionInterval(index0:Int, index1:Int, ?programmatic:Bool = true): Void
Adds the columns from index0
to index1
,
inclusive, to the current selection.
@param programmatic indicate if this is a programmatic change
index0 | one end of the interval |
index1 | the other end of the interval * |
function addColumnSelectionListener(listener:Dynamic ->Void?priority:Int = 0, ?useWeakReference:Bool = false): Void
Adds a listener to column selection changed.
listener | the listener to be add. |
priority | the priority |
useWeakReference | Determines whether the reference to the listener is strong or weak. |
See Also
- org.aswing.event.SelectionEvent
- this.addSelectionListener()
function addRowSelectionInterval(index0:Int, index1:Int, ?programmatic:Bool = true): Void
Adds the rows from index0
to index1
, inclusive, to
the current selection.
@param programmatic indicate if this is a programmatic change
index0 | one end of the interval |
index1 | the other end of the interval * |
function addSelectionListener(listener:Dynamic ->Void?priority:Int = 0, ?useWeakReference:Bool = false): Void
Adds a listener to row selection changed.
listener | the listener to be add. |
priority | the priority |
useWeakReference | Determines whether the reference to the listener is strong or weak. |
See Also
- org.aswing.event.SelectionEvent
function addStateListener(listener:Dynamic ->Void?priority:Int = 0, ?useWeakReference:Bool = false): Void
Add a listener to listen the viewpoat state change event.
When the viewpoat's state changed, the state is all about:
- viewPosition
- verticalUnitIncrement
- verticalBlockIncrement
- horizontalUnitIncrement
- horizontalBlockIncrement
listener | the listener |
priority | the priority |
useWeakReference | Determines whether the reference to the listener is strong or weak. |
See Also
- org.aswing.event.InteractiveEvent.STATE_CHANGED
function changeSelection(rowIndex:Int, columnIndex:Int, toggle:Bool, extend:Bool, ?programmatic:Bool = true): Void
Updates the selection models of the table, depending on the state of the
two flags: toggle
and extend
. Most changes
to the selection that are the result of keyboard or mouse events received
by the UI are channeled through this method so that the behavior may be
overridden by a subclass. Some UIs may need more functionality than
this method provides, such as when manipulating the lead for discontiguous
selection, and may not call into this method for some selection changes.
This implementation uses the following conventions:
-
toggle
: false,extend
: false. Clear the previous selection and ensure the new cell is selected. -
toggle
: false,extend
: true. Extend the previous selection from the anchor to the specified cell, clearing all other selections. -
toggle
: true,extend
: false. If the specified cell is selected, deselect it. If it is not selected, select it. -
toggle
: true,extend
: true. Leave the selection state as it is, but move the anchor index to the specified location.
rowIndex | affects the selection at |
columnIndex | affects the selection at |
toggle | see description above |
extend | if true, extend the current selection * |
function clearSelection(?programmatic:Bool = true): Void
Deselects all selected columns and rows.
programmatic | indicate if this is a programmatic change |
function columnAtPoint(point:IntPoint): Int
Returns the index of the column that point
lies in,
or -1 if the result is not in the range
[0, `getColumnCount()`-1].
point | the location of interest |
returns | the index of the column that |
See Also
- this.rowAtPoint
function convertColumnIndexToModel(viewColumnIndex:Int): Int
Maps the index of the column in the view at
viewColumnIndex
to the index of the column
in the table model. Returns the index of the corresponding
column in the model. If viewColumnIndex
is less than zero, returns viewColumnIndex
.
viewColumnIndex | the index of the column in the view |
returns | the index of the corresponding column in the model |
See Also
function convertColumnIndexToView(modelColumnIndex:Int): Int
Maps the index of the column in the table model at
modelColumnIndex
to the index of the column
in the view. Returns the index of the
corresponding column in the view; returns -1 if this column is not
being displayed. If modelColumnIndex
is less than zero,
returns modelColumnIndex
.
modelColumnIndex | the index of the column in the model |
returns | the index of the corresponding column in the view |
See Also
- this.convertColumnIndexToModel()
function createDefaultColumnsFromModel(): Void
Creates default columns for the table from
the data model using the this.getColumnCount
method
defined in the TableModel
interface.
Clears any existing columns before creating the new columns based on information from the model.
See Also
function editCellAt(row:Int, column:Int, ?clickCount:Int = -1): Bool
editCellAt(row:int, column:int, clickCount:int):Boolean
editCellAt(row:int, column:int):Boolean
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.
row | the row to be edited |
column | the column to be edited |
clickCount | the click count, if force to edite, pass -1 to this param. |
returns | false if for any reason the cell cannot be edited, or if the indices are invalid |
function ensureCellIsVisible(row:Int, column:Int): Void
Scrolls the JTable to make the specified cell completely visible.
row | the row index |
column | the column index |
function getCellPane(): Container
Returns the container that holds the cells.
returns | the container that holds the cells. |
function getCellRect(row:Int, column:Int, includeSpacing:Bool): IntRectangle
Returns a rectangle for the cell that lies at the intersection of
row
and column
.
If includeSpacing
is true then the value returned
has the full height and width of the row and column
specified. If it is false, the returned rectangle is inset by the
intercell spacing to return the true bounds of the rendering or
editing component as it will be set during rendering.
If the column index is valid but the row index is less
than zero the method returns a rectangle with the
y
and height
values set appropriately
and the x
and width
values both set
to zero. In general, when either the row or column indices indicate a
cell outside the appropriate range, the method returns a rectangle
depicting the closest edge of the closest cell that is within
the table's range. When both row and column indices are out
of range the returned rectangle covers the closest
point of the closest cell.
In all cases, calculations that use this method to calculate
results along one axis will not fail because of anomalies in
calculations along the other axis. When the cell is not valid
the includeSpacing
parameter is ignored.
row | the row index where the desired cell is located |
column | 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
|
includeSpacing | if false, return the true cell bounds - computed by subtracting the intercell spacing from the height and widths of the column and row models |
returns | the rectangle containing the cell at location
|
function getColumn(identifier:Dynamic): TableColumn
Returns the TableColumn
object for the column in the table
whose identifier is equal to identifier
, when compared using
equals
.
identifier | the identifier object |
returns | the |
function getColumnAt(columnIndex:Int): TableColumn
Returns the TableColumn
object for the column at
columnIndex
.
columnIndex | the index of the desired column |
returns | the |
function getColumnClass(column:Int): String
Returns the type of the column appearing in the view at
column position column
.
column | the column in the view being queried |
returns | the type of the column at position |
function getColumnCount(): Int
Returns the number of columns in the column model. Note that this may be different from the number of columns in the table model.
returns | the number of columns in the table |
See Also
- this.getRowCount()
- this.removeColumn()
function getColumnName(column:Int): String
Returns the name of the column appearing in the view at
column position column
.
column | the column in the view being queried |
returns | the name of the column at position |
function getDefaultCellFactory(columnClass:String): TableCellFactory
Returns the cell factory to be used when no factory has been set in
a TableColumn
. During the rendering of cells the factory is fetched from
a Maptable
of entries according to the class of the cells in the column. If
there is no entry for this columnClass
the method returns
the entry for the most specific superclass. The JTable
installs entries
for "Object"
, "Number"
, and "Boolean"
, all of which can be modified
or replaced.
columnClass | return the default cell factory for this columnClass |
returns | the factory for this columnClass |
See Also
- this.setDefaultRenderer()
- this.getColumnClass()
function getDefaultEditor(columnClass:String): TableCellEditor
Returns the editor to be used when no editor has been set in
a TableColumn
. During the editing of cells the editor is fetched from
a Maptable
of entries according to the class of the cells in the column. If
there is no entry for this columnClass
the method returns
the entry for the most specific superclass. The JTable
installs entries
for "Object"
, "Number"
, and "Boolean"
, all of which can be modified
or replaced.
columnClass | return the default cell editor for this columnClass |
returns | the default cell editor to be used for this columnClass |
See Also
- this.setDefaultEditor()
- this.getColumnClass()
function getEditingColumn(): Int
Returns the index of the column that contains the cell currently being edited. If nothing is being edited, returns -1.
returns | the index of the column that contains the cell currently being edited; returns -1 if nothing being edited |
See Also
- this.getEditingRow()
function getEditingRow(): Int
Returns the index of the row that contains the cell currently being edited. If nothing is being edited, returns -1.
returns | the index of the row that contains the cell currently being edited; returns -1 if nothing being edited |
See Also
- this.getEditingColumn()
function getLayoutAlignmentX(target:Container): Float
See Also
- LayoutManager.getLayoutAlignmentX return 0
function getLayoutAlignmentY(target:Container): Float
See Also
- LayoutManager.getLayoutAlignmentY return 0
function getLogicLocationFromPixelLocation(p:IntPoint): IntPoint
Returns the logic location in the JTable view area of the pixel location.
function getPixelLocationFromLogicLocation(p:IntPoint): IntPoint
Returns the location in the JTable view area of the logic location.
function getRowCount(): Int
Returns the number of rows in this table's model.
returns | the number of rows in this table's model |
See Also
- this.getColumnCount()
function getSelectedColumn(): Int
Returns the index of the first selected column, -1 if no column is selected.
returns | the index of the first selected column |
function getSelectedColumnCount(): Int
Returns the number of selected columns.
returns | the number of selected columns, 0 if no columns are selected |
function getSelectedColumns(): Array<Dynamic>
Returns the indices of all selected columns.
returns | an array of integers containing the indices of all selected columns, or an empty array if no column is selected |
See Also
- this.getSelectedColumn
function getSelectedRow(): Int
Returns the index of the first selected row, -1 if no row is selected.
returns | the index of the first selected row |
function getSelectedRowCount(): Int
Returns the number of selected rows.
returns | the number of selected rows, 0 if no rows are selected |
function getSelectedRows(): Array<Dynamic>
Returns the indices of all selected rows.
returns | an array of integers containing the indices of all selected rows, or an empty array if no row is selected |
See Also
- this.getSelectedRow
function getSelectionBackground(): ASColor
Returns the background color for selected cells.
returns | the |
See Also
- this.setSelectionBackground()
- this.setSelectionForeground()
function getSelectionForeground(): ASColor
Returns the foreground color for selected cells.
returns | the |
See Also
- this.setSelectionForeground()
- this.setSelectionBackground()
function getValueAt(row:Int, column:Int): Dynamic
Returns the cell value at row
and column
.
Meanwhile the user's actions never affect the model's column ordering.
row | the row whose value is to be queried |
column | the column whose value is to be queried |
returns | the Object at the specified cell |
function isCellEditable(row:Int, column:Int): Bool
Returns true if the cell at row
and column
is editable. Otherwise, invoking this.setValueAt
on the cell
will have no effect.
Meanwhile the user's actions never affect the model's column ordering.
row | the row whose value is to be queried |
column | the column whose value is to be queried |
returns | true if the cell is editable |
See Also
- this.setValueAt()
function isCellSelected(row:Int, column:Int): Bool
Returns true if the specified indices are in the valid range of rows and columns and the cell at the specified position is selected.
row | the row being queried |
column | the column being queried |
returns | true if |
function isColumnSelected(column:Int): Bool
Returns true if the specified index is in the valid range of columns, and the column at that index is selected.
column | the column in the column model |
returns | true if |
Returns true if a cell is being edited.
returns | true if the table is editing a cell |
See Also
- this.getEditingColumn()
- this.getEditingRow()
function isRowSelected(row:Int): Bool
Returns true if the specified index is in the valid range of rows, and the row at that index is selected.
returns | true if |
function moveColumn(column:Int, targetColumn:Int): Void
Moves the column column
to the position currently
occupied by the column targetColumn
in the view.
The old column at targetColumn
is
shifted left or right to make room.
column | the index of column to be moved |
targetColumn | the new index of the column |
function removeColumn(aColumn:TableColumn): Void
Removes aColumn
from this JTable
's
array of columns. Note: this method does not remove the column
of data from the model; it just removes the TableColumn
that was responsible for displaying it.
@param aColumn the TableColumn
to be removed
@see #addColumn
function removeColumnSelectionInterval(index0:Int, index1:Int, ?programmatic:Bool = true): Void
Deselects the columns from index0
to index1
, inclusive.
index0 | one end of the interval |
index1 | the other end of the interval |
programmatic | indicate if this is a programmatic change |
function removeColumnSelectionListener(listener:Dynamic ->Void): Void
Removes a listener from column selection changed listeners.
listener | the listener to be removed. |
See Also
- org.aswing.event.SelectionEvent
- this.removeSelectionListener()
function removeRowSelectionInterval(index0:Int, index1:Int, ?programmatic:Bool = true): Void
Deselects the rows from index0
to index1
, inclusive.
index0 | one end of the interval |
index1 | the other end of the interval |
programmatic | indicate if this is a programmatic change |
function removeSelectionListener(listener:Dynamic ->Void): Void
Removes a listener from row selection changed listeners.
listener | the listener to be removed. |
See Also
- org.aswing.event.SelectionEvent
function removeStateListener(listener:Dynamic ->Void): Void
Removes a state listener.
listener | the listener to be removed. |
See Also
- org.aswing.event.InteractiveEvent.STATE_CHANGED
function rowAtPoint(point:IntPoint): Int
Returns the index of the row that point
lies in,
or -1 if the result is not in the range
[0, `getRowCount()`-1].
point | the location of interest |
returns | the index of the row that |
See Also
- this.columnAtPoint
function selectAll(?programmatic:Bool = true): Void
Selects all rows, columns, and cells in the table.
programmatic | indicate if this is a programmatic change |
function setColumnSelectionInterval(index0:Int, index1:Int, ?programmatic:Bool = true): Void
Selects the columns from index0
to index1
,
inclusive.
index0 | one end of the interval |
index1 | the other end of the interval |
programmatic | indicate if this is a programmatic change |
function setDefaultCellFactory(columnClass:String, renderer:TableCellFactory): Void
Sets a default cell factory to be used if no renderer has been set in
a TableColumn
. If renderer is null
,
removes the default factory for this column class.
columnClass | set the default cell factory for this columnClass |
renderer | default cell factory to be used for this columnClass |
See Also
- this.getDefaultRenderer()
- this.setDefaultEditor()
function setDefaultEditor(columnClass:String, editor:TableCellEditor): Void
Sets a default cell editor to be used if no editor has been set in
a TableColumn
. If no editing is required in a table, or a
particular column in a table, uses the isCellEditable
method in the TableModel
interface to ensure that this
JTable
will not start an editor in these columns.
If editor is null
, removes the default editor for this
column class.
columnClass | set the default cell editor for this columnClass |
editor | default cell editor to be used for this columnClass |
See Also
- TableModel.isCellEditable()
- this.getDefaultEditor()
- this.setDefaultRenderer()
function setRowSelectionInterval(index0:Int, index1:Int, ?programmatic:Bool = true): Void
Selects the rows from index0
to index1
,
inclusive.
index0 | one end of the interval |
index1 | the other end of the interval |
programmatic | indicate if this is a programmatic change |
function setSelectionBackground(selectionBackground:ASColor): Void
Sets the background color for selected cells. Cell renderers can use this color to the fill selected cells.
The default value of this property is defined by the look and feel implementation.
selectionBackground | the |
See Also
- this.getSelectionBackground()
- this.setSelectionForeground()
- this.setForeground()
- this.setBackground()
- this.setFont()
function setSelectionForeground(selectionForeground:ASColor): Void
Sets the foreground color for selected cells. Cell renderers can use this color to render text and graphics for selected cells.
The default value of this property is defined by the look and feel implementation.
selectionForeground | the |
See Also
- this.getSelectionForeground()
- this.setSelectionBackground()
- this.setForeground()
- this.setBackground()
- this.setFont()
function setValueAt(aValue:Dynamic, row:Int, column:Int): Void
Sets the value for the cell in the table model at row
and column
.
aValue
is the new value.
aValue | the new value |
row | the row of the cell to be changed |
column | the column of the cell to be changed |
See Also
- this.getValueAt()