Spec-Zone .ru
спецификации, руководства, описания, API
|
|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.JTable
JTable is a user-interface component that presents data in a two-dimensional table format. 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 new Integer(row*col); } }; JTable table = new JTable(dataModel); JScrollPane scrollpane = new JScrollPane(table);
Because the JTable is now much easier to set up with custom models the DefaultTableModel is less useful than it was in previous releases. Instead of copying the data in an application into the DefaultTableModel, we recommend wrapping it in the methods of the TableModel interface and passing the real data to the JTable as above. This technique is nearly as concise as using a DefaultTableModel and starting this way has a number of advantages over the longer term. In particular: it is a scalable technique, is easier to handle dynamic or editable tables and often results in much more efficient applications because the model is free to choose the internal representation that best suits the data.
The "Table" directory in the examples/demo area 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. For example, overriding
AbstractTableModel's getColumnClass()
method to return a value of
ImageIcon.class
for a given column allows icons to be displayed,
while returning a value of Number.class
allows digits to be
right-justified in the column.
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 and display the appropriate
values from the model.
If getTableHeader().setReorderingAllowed(boolean)
is used to
enable column reordering 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 the model will be queried in its own co-ordinate 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 co-ordinate system where the order of the rows is changed, rather than the order of the columns.
The general rule for the JTable API and the APIs of all its associated classes, including the the column model and both the row and column selection models, is: methods using integer indices for rows and columns always use the co-ordinate system of the view. There are three exceptions to this rule:
The convertColumnIndexToView() and convertColumnIndexToModel() methods have been provided to convert between the two co-ordinate systems but they are rarely needed during normal use.
Like all JComponent classes, you can use
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)
to associate an
Action
object with a KeyStroke
and execute the
action under specified conditions.
See
For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JTable key assignments.
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. A future release of Swing will provide support for long term persistence.
Inner Class Summary | |
protected class |
JTable.AccessibleJTable
The class used to obtain the accessible role for this object. |
Inner classes inherited from class javax.swing.JComponent |
JComponent.AccessibleJComponent |
Field Summary | |
static int |
AUTO_RESIZE_ALL_COLUMNS
During all resize operations, proportionately resize all columns |
static int |
AUTO_RESIZE_LAST_COLUMN
During all resize operations, apply adjustments to the last column only |
static int |
AUTO_RESIZE_NEXT_COLUMN
When a column is adjusted in the UI, adjust the next column the opposite way |
static int |
AUTO_RESIZE_OFF
Do not adjust column widths automatically, use a scrollbar |
static int |
AUTO_RESIZE_SUBSEQUENT_COLUMNS
During UI adjustment, change subsequent columns to preserve the total width |
protected boolean |
autoCreateColumnsFromModel
The table will query the TableModel to build the default set of columns if this is true. |
protected int |
autoResizeMode
This mode value determines if table automatically resizes the width the table's columns to take up the entire width of the table, and how it does the resizing. |
protected TableCellEditor |
cellEditor
The object that overwrites the screen real estate occupied by the current cell and allows the user to change those contents. |
protected boolean |
cellSelectionEnabled
If this is true, then both a row selection and a column selection can be non-empty at the same time, the selected cells are the the cells whose row and column are both selected. |
protected TableColumnModel |
columnModel
The TableColumnModel of the table |
protected TableModel |
dataModel
The TableModel of the table |
protected Hashtable |
defaultEditorsByColumnClass
A table of objects that display and edit the contents of a cell, indexed by class. |
protected Hashtable |
defaultRenderersByColumnClass
A table of objects that display the contents of a cell, indexed by class. |
protected int |
editingColumn
Identifies the column of the cell being edited. |
protected int |
editingRow
Identifies the row of the cell being edited. |
protected Component |
editorComp
If editing, Component that is handling the editing. |
protected Color |
gridColor
The color of the grid |
protected Dimension |
preferredViewportSize
Used by the Scrollable interface to determine the initial visible area |
protected int |
rowHeight
The height of all rows in the table |
protected int |
rowMargin
The height margin between rows |
protected boolean |
rowSelectionAllowed
Row selection 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 |
protected JTableHeader |
tableHeader
The TableHeader working with the table |
Fields inherited from class javax.swing.JComponent |
accessibleContext,
listenerList,
TOOL_TIP_TEXT_KEY,
ui,
UNDEFINED_CONDITION,
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
WHEN_FOCUSED,
WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT,
CENTER_ALIGNMENT,
LEFT_ALIGNMENT,
RIGHT_ALIGNMENT,
TOP_ALIGNMENT |
Constructor Summary | |
JTable()
Constructs a default JTable which is initialized with a default data model, a default column model, and a default selection model. |
|
JTable(int numRows,
int numColumns)
Constructs a JTable with numRows and numColumns of empty cells using the DefaultTableModel. |
|
JTable(Object[][] rowData,
Object[] columnNames)
Constructs a JTable to display the values in the two dimensional array, rowData, with column names, columnNames. |
|
JTable(TableModel dm)
Constructs a JTable which is initialized with dm as the data model, a default column model, and a default selection model. |
|
JTable(TableModel dm,
TableColumnModel cm)
Constructs a JTable which is initialized with dm as the data model, cm as the column model, and a default selection model. |
|
JTable(TableModel dm,
TableColumnModel cm,
ListSelectionModel sm)
Constructs a JTable which is initialized with dm as the data model, cm as the column model, and sm as the selection model. |
|
JTable(Vector rowData,
Vector columnNames)
Constructs a JTable to display the values in the Vector of Vectors, rowData, with column names, columnNames. |
Method Summary | |
void |
addColumn(TableColumn aColumn)
Appends aColumn to the end of the array of columns held by the JTable's column model. |
void |
addColumnSelectionInterval(int index0,
int index1)
Adds the columns from index0 to index0 inclusive to the current selection. |
void |
addNotify()
Calls configureEnclosingScrollPane . |
void |
addRowSelectionInterval(int index0,
int index1)
Adds the rows from index0 to index0 inclusive to the current selection. |
void |
clearSelection()
Deselects all selected columns and rows. |
void |
columnAdded(TableColumnModelEvent e)
Tells listeners that a column was added to the model. |
int |
columnAtPoint(Point point)
Returns the index of the column that point lies in, or -1 if it lies outside the receiver's bounds. |
void |
columnMarginChanged(ChangeEvent e)
Tells listeners that a column was moved due to a margin change. |
void |
columnMoved(TableColumnModelEvent e)
Tells listeners that a column was repositioned. |
void |
columnRemoved(TableColumnModelEvent e)
Tells listeners that a column was removed from the model. |
void |
columnSelectionChanged(ListSelectionEvent e)
Tells listeners that the selection model of the TableColumnModel changed. |
protected void |
configureEnclosingScrollPane()
If the 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 scrollpane. |
int |
convertColumnIndexToModel(int viewColumnIndex)
Return the index of the column in the model whose data is being displayed in the column viewColumnIndex in the display. |
int |
convertColumnIndexToView(int modelColumnIndex)
Return the index of the column in the view which is displaying the data from the column modelColumnIndex in the model. |
protected TableColumnModel |
createDefaultColumnModel()
Returns the default column model object which is a DefaultTableColumnModel. |
void |
createDefaultColumnsFromModel()
This method will create default columns for the table from the data model using the getColumnCount() and getColumnClass() methods defined in the TableModel interface. |
protected TableModel |
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()
|
protected ListSelectionModel |
createDefaultSelectionModel()
Returns the default selection model object which is a DefaultListSelectionModel. |
protected JTableHeader |
createDefaultTableHeader()
Returns the default table header object which is a JTableHeader. |
static JScrollPane |
createScrollPaneForTable(JTable aTable)
Deprecated. As of Swing version 1.0.2, replaced by new JScrollPane(aTable) . |
boolean |
editCellAt(int row,
int column)
Programmatically starts editing the cell at row and column, if the cell is editable. |
boolean |
editCellAt(int row,
int column,
EventObject e)
Programmatically starts editing the cell at row and column, if the cell is editable. |
void |
editingCanceled(ChangeEvent e)
Invoked when editing is canceled. |
void |
editingStopped(ChangeEvent e)
Invoked when editing is finished. |
AccessibleContext |
getAccessibleContext()
Get the AccessibleContext associated with this JComponent |
boolean |
getAutoCreateColumnsFromModel()
Returns whether the table will create default columns from the model. |
int |
getAutoResizeMode()
Returns auto resize mode of the table. |
TableCellEditor |
getCellEditor()
Return the cellEditor. |
TableCellEditor |
getCellEditor(int row,
int column)
Return an appropriate editor for the cell specified by this this row and column. |
Rectangle |
getCellRect(int row,
int column,
boolean includeSpacing)
Returns a rectangle locating the cell that lies at the intersection of row and column. |
TableCellRenderer |
getCellRenderer(int row,
int column)
Return an appropriate renderer for the cell specified by this this row and column. |
boolean |
getCellSelectionEnabled()
Returns true if simultaneous row and column selections are allowed |
TableColumn |
getColumn(Object identifier)
Returns the TableColumn object for the column in the table whose identifier is equal to identifier, when compared using equals(). |
Class |
getColumnClass(int column)
Returns the type of the column at the specified view position. |
int |
getColumnCount()
Returns the number of columns in the column model, note this may be different to the number of columns in the table model. |
TableColumnModel |
getColumnModel()
Returns the TableColumnModel that contains all column inforamtion of this table. |
String |
getColumnName(int column)
Returns the name of the column at the specified view position. |
boolean |
getColumnSelectionAllowed()
Returns true if columns can be selected. |
TableCellEditor |
getDefaultEditor(Class columnClass)
Returns the editor to be used when no editor has been set in a TableColumn. |
TableCellRenderer |
getDefaultRenderer(Class columnClass)
Returns the renderer to be used when no renderer has been set in a TableColumn. |
int |
getEditingColumn()
This returns the index of the editing column. |
int |
getEditingRow()
Returns the index of the editing row. |
Component |
getEditorComponent()
If the receiver is currently editing this will return the Component that was returned from the CellEditor. |
Color |
getGridColor()
Returns the color used to draw grid lines. |
Dimension |
getIntercellSpacing()
Returns the horizontal and vertical spacing between cells. |
TableModel |
getModel()
Returns the TableModel that provides the data displayed by the receiver. |
Dimension |
getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for this table. |
int |
getRowCount()
Returns the number of rows in the table. |
int |
getRowHeight()
Returns the height of a table row in the receiver. |
int |
getRowMargin()
Gets the amount of emtpy space between rows. |
boolean |
getRowSelectionAllowed()
Returns true if rows can be selected. |
int |
getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns The visibleRect.height or visibleRect.width, depending on the table's orientation. |
boolean |
getScrollableTracksViewportHeight()
Returns false to indicate that the height of the viewport does not determine the height of the table. |
boolean |
getScrollableTracksViewportWidth()
Returns false to indicate that the width of the viewport does not determine the width of the table. |
int |
getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns the scroll increment that completely exposes one new row or column (depending on the orientation). |
int |
getSelectedColumn()
Returns the index of the first selected column, -1 if no column is selected. |
int |
getSelectedColumnCount()
Returns the number of selected columns. |
int[] |
getSelectedColumns()
Returns the indices of all selected columns. |
int |
getSelectedRow()
Returns the index of the first selected row, -1 if no row is selected. |
int |
getSelectedRowCount()
Returns the number of selected rows. |
int[] |
getSelectedRows()
Returns the indices of all selected rows. |
Color |
getSelectionBackground()
Returns the background color for selected cells. |
Color |
getSelectionForeground()
Returns the foreground color for selected cells. |
ListSelectionModel |
getSelectionModel()
Returns the ListSelectionModel that is used to maintain row selection state. |
boolean |
getShowHorizontalLines()
Returns true if the receiver draws horizontal lines between cells, false if it doesn't. |
boolean |
getShowVerticalLines()
Returns true if the receiver draws vertical lines between cells, false if it doesn't. |
JTableHeader |
getTableHeader()
Returns the tableHeader working with this JTable. |
String |
getToolTipText(MouseEvent event)
Overrides JComponent's setToolTipText method to allow use of the renderer's tips (if the renderer has text set). |
TableUI |
getUI()
Returns the L&F object that renders this component. |
String |
getUIClassID()
Returns the name of the L&F class that renders this component. |
Object |
getValueAt(int row,
int column)
Returns the cell value at row and column. |
protected void |
initializeLocalVars()
Initializes table properties to their default values. |
boolean |
isCellEditable(int row,
int column)
Returns true if the cell at row and column is editable. |
boolean |
isCellSelected(int row,
int column)
Returns true if the cell at the specified position is selected. |
boolean |
isColumnSelected(int column)
Returns true if the column at the specified index is selected |
boolean |
isEditing()
Returns true is the table is editing a cell. |
boolean |
isManagingFocus()
We override this method, whose implementation in JComponent returns false, to return true. |
boolean |
isRowSelected(int row)
Returns true if the row at the specified index is selected |
void |
moveColumn(int column,
int targetColumn)
Moves the column column to the position currently occupied by the column targetColumn. |
protected String |
paramString()
Returns a string representation of this JTable. |
Component |
prepareEditor(TableCellEditor editor,
int row,
int column)
Prepares the specified editor using the value at the specified cell. |
Component |
prepareRenderer(TableCellRenderer renderer,
int row,
int column)
Prepares the specified renderer with an appropriate value from the dataModel, and an appropriate selection value from the selection models. |
void |
removeColumn(TableColumn aColumn)
Removes aColumn from the JTable's array of columns. |
void |
removeColumnSelectionInterval(int index0,
int index1)
Deselects the columns from index0 to index0 inclusive. |
void |
removeEditor()
Discard the editor object and return the real estate it used to cell rendering. |
void |
removeRowSelectionInterval(int index0,
int index1)
Deselects the rows from index0 to index0 inclusive. |
void |
reshape(int x,
int y,
int width,
int height)
Calls super.reshape(), and is overridden simply to detect changes in our bounds. |
protected void |
resizeAndRepaint()
Equivalent to revalidate() followed by repaint(). |
int |
rowAtPoint(Point point)
Returns the index of the row that point lies in, or -1 if is not in the range [0, getRowCount()-1]. |
void |
selectAll()
Select all rows, columns and cells in the table. |
void |
setAutoCreateColumnsFromModel(boolean createColumns)
Sets the table's autoCreateColumnsFromModel flag. |
void |
setAutoResizeMode(int mode)
Sets the table's auto resize mode when the table is resized. |
void |
setCellEditor(TableCellEditor anEditor)
Set the cellEditor variable. |
void |
setCellSelectionEnabled(boolean flag)
Sets whether this table allows both a column selection and a row selection to exist at the same time. |
void |
setColumnModel(TableColumnModel newModel)
Sets the column model for this table to newModel and registers with for listner notifications from the new column model. |
void |
setColumnSelectionAllowed(boolean flag)
Sets whether the columns in this model can be selected. |
void |
setColumnSelectionInterval(int index0,
int index1)
Selects the columns from index0 to index1 inclusive. |
void |
setDefaultEditor(Class columnClass,
TableCellEditor editor)
Set a default editor to be used if no editor has been set in a TableColumn. |
void |
setDefaultRenderer(Class columnClass,
TableCellRenderer renderer)
Set a default renderer to be used if no renderer has been set in a TableColumn. |
void |
setEditingColumn(int aColumn)
Set the editingColumn variable. |
void |
setEditingRow(int aRow)
Set the editingRow variable. |
void |
setGridColor(Color newColor)
Sets the color used to draw grid lines to color and redisplays the receiver. |
void |
setIntercellSpacing(Dimension newSpacing)
Sets the width and height between cells to newSpacing and redisplays the receiver. |
void |
setModel(TableModel newModel)
Sets the data model for this table to newModel and registers with for listner notifications from the new data model. |
void |
setPreferredScrollableViewportSize(Dimension size)
Sets the preferred size of the viewport for this table. |
void |
setRowHeight(int newHeight)
Sets the height for rows to newRowHeight and invokes tile |
void |
setRowMargin(int rowMargin)
Sets the amount of emtpy space between rows. |
void |
setRowSelectionAllowed(boolean flag)
Sets whether the rows in this model can be selected. |
void |
setRowSelectionInterval(int index0,
int index1)
Selects the rows from index0 to index1 inclusive. |
void |
setSelectionBackground(Color selectionBackground)
Set the background color for selected cells. |
void |
setSelectionForeground(Color selectionForeground)
Set the foreground color for selected cells. |
void |
setSelectionMode(int selectionMode)
Sets the table's selection mode to allow only single selections, a single contiguous interval, or multiple intervals. |
void |
setSelectionModel(ListSelectionModel newModel)
Sets the row selection model for this table to newModel and registers with for listner notifications from the new selection model. |
void |
setShowGrid(boolean b)
Sets whether the receiver draws grid lines around cells. |
void |
setShowHorizontalLines(boolean b)
Sets whether the receiver draws horizontal lines between cells. |
void |
setShowVerticalLines(boolean b)
Sets whether the receiver draws vertical lines between cells. |
void |
setTableHeader(JTableHeader newHeader)
Sets the tableHeader working with this JTable to newHeader. |
void |
setUI(TableUI ui)
Sets the L&F object that renders this component. |
void |
setValueAt(Object aValue,
int row,
int column)
Sets the value for the cell at row and column. |
void |
sizeColumnsToFit(boolean lastColumnOnly)
Deprecated. As of Swing version 1.0.3, replaced by sizeColumnsToFit(int) . |
void |
sizeColumnsToFit(int resizingColumn)
This method will resize one or more ot the columns in the table so that the total width of all of the JTable's columns will be equal to the width of the table. |
void |
tableChanged(TableModelEvent e)
The TableModelEvent should be constructed in the co-ordinate system of the model, the appropriate mapping to the view co-ordinate system is performed by the JTable when it recieves the event. |
void |
updateUI()
Notification from the UIManager that the L&F has changed. |
void |
valueChanged(ListSelectionEvent e)
Invoked when the selection changes -- repaints to show the new selection. |
Methods inherited from class java.awt.Container |
add,
add,
add,
add,
add,
addContainerListener,
addImpl,
countComponents,
deliverEvent,
doLayout,
findComponentAt,
findComponentAt,
getComponent,
getComponentAt,
getComponentAt,
getComponentCount,
getComponents,
getLayout,
insets,
invalidate,
isAncestorOf,
layout,
list,
list,
locate,
minimumSize,
paintComponents,
preferredSize,
print,
printComponents,
processContainerEvent,
processEvent,
remove,
remove,
removeAll,
removeContainerListener,
setCursor,
setLayout,
validate,
validateTree |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Field Detail |
public static final int AUTO_RESIZE_OFF
public static final int AUTO_RESIZE_NEXT_COLUMN
public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS
public static final int AUTO_RESIZE_LAST_COLUMN
public static final int AUTO_RESIZE_ALL_COLUMNS
protected TableModel dataModel
protected TableColumnModel columnModel
protected ListSelectionModel selectionModel
protected JTableHeader tableHeader
protected int rowHeight
protected int rowMargin
protected Color gridColor
protected boolean showHorizontalLines
protected boolean showVerticalLines
protected int autoResizeMode
protected boolean autoCreateColumnsFromModel
protected Dimension preferredViewportSize
protected boolean rowSelectionAllowed
protected boolean cellSelectionEnabled
protected transient Component editorComp
protected transient TableCellEditor cellEditor
protected transient int editingColumn
protected transient int editingRow
protected transient Hashtable defaultRenderersByColumnClass
protected transient Hashtable defaultEditorsByColumnClass
protected Color selectionForeground
protected Color selectionBackground
Constructor Detail |
public JTable()
createDefaultDataModel()
,
createDefaultColumnModel()
,
createDefaultSelectionModel()
public JTable(TableModel dm)
dm
- The data model for the tablecreateDefaultColumnModel()
,
createDefaultSelectionModel()
public JTable(TableModel dm, TableColumnModel cm)
dm
- The data model for the tablecm
- The column model for the tablecreateDefaultSelectionModel()
public JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
dm
- The data model for the tablecm
- The column model for the tablesm
- The row selection model for the tablecreateDefaultDataModel()
,
createDefaultColumnModel()
,
createDefaultSelectionModel()
public JTable(int numRows, int numColumns)
numRows
- The number of rows the table holdsnumColumns
- The number of columns the table holdsDefaultTableModel
public JTable(Vector rowData, Vector columnNames)
((Vector)rowData.elementAt(1)).elementAt(5);
All rows must be of the same length as columnNames.
rowData
- The data for the new tablecolumnNames
- Names of each columnpublic JTable(Object[][] rowData, Object[] columnNames)
rowData[1][5];
All rows must be of the same length as columnNames.
rowData
- The data for the new tablecolumnNames
- Names of each columnMethod Detail |
public void addNotify()
configureEnclosingScrollPane
.configureEnclosingScrollPane()
protected void configureEnclosingScrollPane()
new JScrollPane(myTable)
, addNotify
is
called in the JTable (when the table is added to the viewport).
JTable's addNotify
method in turn calls this method
which is protected so that this default installation procedure can
be overridden by a subclass.addNotify()
public static JScrollPane createScrollPaneForTable(JTable aTable)
new JScrollPane(aTable)
.
new JScrollPane(aTable)
.public void setTableHeader(JTableHeader newHeader)
newHeader
- new tableHeadergetTableHeader()
public JTableHeader getTableHeader()
setTableHeader(javax.swing.table.JTableHeader)
public void setRowHeight(int newHeight)
newRowHeight
- new row heightgetRowHeight()
public int getRowHeight()
setRowHeight(int)
public void setRowMargin(int rowMargin)
getRowMargin()
public int getRowMargin()
getIntercellSpacing().height
.setRowMargin(int)
public void setIntercellSpacing(Dimension newSpacing)
newSpacing
- The new width and height intercellSpacinggetIntercellSpacing()
public Dimension getIntercellSpacing()
setIntercellSpacing(java.awt.Dimension)
public void setGridColor(Color newColor)
color
- new color of the gridgetGridColor()
public Color getGridColor()
setGridColor(java.awt.Color)
public void setShowGrid(boolean b)
flag
- true if table view should draw grid linessetShowVerticalLines(boolean)
,
setShowHorizontalLines(boolean)
public void setShowHorizontalLines(boolean b)
flag
- true if table view should draw horizontal linesgetShowHorizontalLines()
,
setShowGrid(boolean)
,
setShowVerticalLines(boolean)
public void setShowVerticalLines(boolean b)
flag
- true if table view should draw vertical linesgetShowVerticalLines()
,
setShowGrid(boolean)
,
setShowHorizontalLines(boolean)
public boolean getShowHorizontalLines()
setShowHorizontalLines(boolean)
public boolean getShowVerticalLines()
setShowVerticalLines(boolean)
public void setAutoResizeMode(int mode)
mode
- One of 5 legal values:
AUTO_RESIZE_OFF,
AUTO_RESIZE_NEXT_COLUMN,
AUTO_RESIZE_SUBSEQUENT_COLUMNS,
AUTO_RESIZE_LAST_COLUMN,
AUTO_RESIZE_ALL_COLUMNSgetAutoResizeMode()
,
sizeColumnsToFit(int)
public int getAutoResizeMode()
setAutoResizeMode(int)
,
sizeColumnsToFit(int)
public void setAutoCreateColumnsFromModel(boolean createColumns)
createColumns
- true if JTable should auto create columnsgetAutoCreateColumnsFromModel()
,
createDefaultColumnsFromModel()
public boolean getAutoCreateColumnsFromModel()
setAutoCreateColumnsFromModel(boolean)
,
createDefaultColumnsFromModel()
public void createDefaultColumnsFromModel()
This method will clear any exsiting columns before creating the new columns based on information from the model.
getAutoCreateColumnsFromModel()
public void setDefaultRenderer(Class columnClass, TableCellRenderer renderer)
getDefaultRenderer(java.lang.Class)
,
setDefaultEditor(java.lang.Class, javax.swing.table.TableCellEditor)
public TableCellRenderer getDefaultRenderer(Class columnClass)
setDefaultRenderer(java.lang.Class, javax.swing.table.TableCellRenderer)
,
getColumnClass(int)
public void setDefaultEditor(Class columnClass, TableCellEditor editor)
TableModel.isCellEditable(int, int)
,
getDefaultEditor(java.lang.Class)
,
setDefaultRenderer(java.lang.Class, javax.swing.table.TableCellRenderer)
public TableCellEditor getDefaultEditor(Class columnClass)
setDefaultEditor(java.lang.Class, javax.swing.table.TableCellEditor)
,
getColumnClass(int)
public void setSelectionMode(int selectionMode)
Both the row and column selection models for the JTable default to using a DefaultListSelectionModel so that JTable works the same way as the JList. See setSelectionMode() in JList for details about the modes.
JList.setSelectionMode(int)
public void setRowSelectionAllowed(boolean flag)
getRowSelectionAllowed()
public boolean getRowSelectionAllowed()
setRowSelectionAllowed(boolean)
public void setColumnSelectionAllowed(boolean flag)
getColumnSelectionAllowed()
public boolean getColumnSelectionAllowed()
setColumnSelectionAllowed(boolean)
public void setCellSelectionEnabled(boolean flag)
getCellSelectionEnabled()
public boolean getCellSelectionEnabled()
setCellSelectionEnabled(boolean)
public void selectAll()
public void clearSelection()
public void setRowSelectionInterval(int index0, int index1)
index0
- one end of the interval.index1
- other end of the intervalpublic void setColumnSelectionInterval(int index0, int index1)
index0
- one end of the interval.index1
- other end of the intervalpublic void addRowSelectionInterval(int index0, int index1)
index0
- one end of the interval.index1
- other end of the intervalpublic void addColumnSelectionInterval(int index0, int index1)
index0
- one end of the interval.index1
- other end of the intervalpublic void removeRowSelectionInterval(int index0, int index1)
index0
- one end of the interval.index1
- other end of the intervalpublic void removeColumnSelectionInterval(int index0, int index1)
index0
- one end of the interval.index1
- other end of the intervalpublic int getSelectedRow()
public int getSelectedColumn()
public int[] getSelectedRows()
getSelectedRow()
public int[] getSelectedColumns()
getSelectedColumn()
public int getSelectedRowCount()
public int getSelectedColumnCount()
public boolean isRowSelected(int row)
public boolean isColumnSelected(int column)
public boolean isCellSelected(int row, int column)
public Color getSelectionForeground()
setSelectionForeground(java.awt.Color)
,
setSelectionBackground(java.awt.Color)
public void setSelectionForeground(Color selectionForeground)
The default value of this property is defined by the look and feel implementation.
This is a JavaBeans bound property.
selectionForeground
- the Color to use in the foreground
for selected list itemsgetSelectionForeground()
,
setSelectionBackground(java.awt.Color)
,
JComponent.setForeground(java.awt.Color)
,
JComponent.setBackground(java.awt.Color)
,
JComponent.setFont(java.awt.Font)
public Color getSelectionBackground()
setSelectionBackground(java.awt.Color)
,
setSelectionForeground(java.awt.Color)
public void setSelectionBackground(Color selectionBackground)
The default value of this property is defined by the look and feel implementation.
This is a JavaBeans bound property.
selectionBackground
- the Color to use for the background
of selected cellsgetSelectionBackground()
,
setSelectionForeground(java.awt.Color)
,
JComponent.setForeground(java.awt.Color)
,
JComponent.setBackground(java.awt.Color)
,
JComponent.setFont(java.awt.Font)
public TableColumn getColumn(Object identifier)
identifier
- the identifier objectpublic int convertColumnIndexToModel(int viewColumnIndex)
convertColumnIndexToView(int)
public int convertColumnIndexToView(int modelColumnIndex)
convertColumnIndexToModel(int)
public int getRowCount()
getColumnCount()
public int getColumnCount()
getRowCount()
public String getColumnName(int column)
public Class getColumnClass(int column)
public Object getValueAt(int row, int column)
NOTE: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, what is at column 2 changes. Meanwhile the user's actions never affect the model's column ordering.
row
- the row whose value is to be looked upcolumn
- the column whose value is to be looked uppublic void setValueAt(Object aValue, int row, int column)
aValue
- the new valuerow
- the row whose value is to be changedcolumn
- the column whose value is to be changedgetValueAt(int, int)
public boolean isCellEditable(int row, int column)
row
- the row whose value is to be looked upcolumn
- the column whose value is to be looked upsetValueAt(java.lang.Object, int, int)
public void addColumn(TableColumn aColumn)
getModel().getColumnName()
.
To add a column to the JTable to display the modelColumn'th column of data in the model, with a given width, cellRenderer and cellEditor you can use:
addColumn(new TableColumn(modelColumn, width, cellRenderer, cellEditor));[All of the other constructors in the TableColumn can be used in place of this one.] The model column is stored inside the TableColumn and is used during rendering and editing to locate the appropriate data values in the model. The model column does not change when columns are reordered in the view.
aColumn
- The TableColumn to be addedremoveColumn(javax.swing.table.TableColumn)
public void removeColumn(TableColumn aColumn)
aColumn
- The TableColumn to be removedaddColumn(javax.swing.table.TableColumn)
public void moveColumn(int column, int targetColumn)
column
- the index of column to be movedtargetColumn
- the new index of the columnpublic int columnAtPoint(Point point)
rowAtPoint(java.awt.Point)
public int rowAtPoint(Point point)
columnAtPoint(java.awt.Point)
public Rectangle getCellRect(int row, int column, boolean includeSpacing)
row
- the row to computecolumn
- the column to computeincludeSpacing
- if true, the rect returned will
include the correct
intercellSpacingpublic void reshape(int x, int y, int width, int height)
sizeColumnsToFit(int)
public void sizeColumnsToFit(boolean lastColumnOnly)
sizeColumnsToFit(int)
.
sizeColumnsToFit(int)
public void sizeColumnsToFit(int resizingColumn)
When setBounds() is called on the JTable, often as
a result of resizing of an enclosing window -
this method is called with resizingColumn
set to -1. This means that resizing has taken place 'outside' the
JTable and the change - or 'delta' - should be distributed to all
of the columns regardless of the JTable's autoResizeMode mode.
If the resizingColumn
is not -1, it is one of
the columns in the table that has changed size rather than
the table itself. In this case the auto-resize modes govern
the way the extra (or defecit) space is distributed
amongst the availible columns.
The modes are:
Note: When the JTable makes adjustments to the widths of the columns it respects their minimum and maximum values absolutely. It is therefore possible that, even after this method is called, the total width of the columns is still not equal to the width of the table. When this happens the JTable does not put itself in AUTO_RESIZE_OFF mode to bring up a ScrollBar, or break other commitments of its current auto-resize mode - instead it allows its bounds to be set larger (or smaller) than the total of the column minima or maxima, meaning, either that there will not be enough room to display all of the columns, or that the columns will not fill the JTable's bounds. These respectively, result in the clipping of some columns or an area being painted in the JTable's background color during painting.
The mechanism for distributing the delta amongst the availible columns is provided in a private method in the JTable class:
adjustSizes(long targetSize, final Resizable3 r, boolean inverse)an explanation of which is provided below. Resizable3 is a private interface that allows any data structure containing a collection of elements with a size, preferredSize, maximumSize and minimumSize to have its elements manipulated by the algorithm.
Call 'DELTA' the difference between the targetSize and the sum of the preferred sizes of the elements in r. The individual sizes are calculated by taking the original preferred sizes and adding a share of the DELTA - that share being based on how far each preferred size is from its limiting bound (minimum or maximum).
Call the individual constraints min[i], max[i] and pref[i].
Call their respective sums: MIN, MAX and PREF.
Each new size will be calculated using:
size[i] = pref[i] + delta[i]where each individual delta[i] is calculated according to:
If (DELTA < 0) we are in shrink mode where:
DELTA delta[i] = ------------ * (pref[i] - min[i]) (PREF - MIN)If (DELTA > 0) we are in expand mode where:
DELTA delta[i] = ------------ * (max[i] - pref[i]) (MAX - PREF)
The overall effect is that the total size moves that same percentage, k, towards the total minimum or maximum and that percentage guarentees accomodation of the required space, DELTA.
Naive evaluation of the formulae presented here would be subject to
the aggregated rounding errors caused by doing this operation in finite
precision (using ints). To deal with this, the muliplying factor above,
is constantly recalculated and this takes account of the rounding errors in
the previous iterations. The result is an algorithm which produces
a set of integers whose values exactly sum to the supplied
targetSize
, and does so by spreading the rounding
errors evenly over the given elements.
When targetSize
is outside the [MIN, MAX] range, the algorithm
sets all sizes to either their appropriate limiting value (maximum or minimum).
resizingColumn
- The column whose resizing made this adjustment
necessary or -1 if there is no such column.TableColumn.setWidth(int)
public String getToolTipText(MouseEvent event)
NOTE: For JTable to properly display tooltips of its renderers JTable must be a registered component with the ToolTipManager. This is done automatically in initializeLocalVars(), but if at a later point JTable is told setToolTipText(null) it will unregister the table component, and no tips from renderers will display anymore.
JComponent.getToolTipText()
public boolean editCellAt(int row, int column)
row
- the row to be editedcolumn
- the column to be editedpublic boolean editCellAt(int row, int column, EventObject e)
row
- the row to be editedcolumn
- the column to be editede
- event to pass into
shouldSelectCellpublic boolean isEditing()
editingColumn
,
editingRow
public Component getEditorComponent()
public int getEditingColumn()
editingRow
public int getEditingRow()
editingColumn
public TableUI getUI()
public void setUI(TableUI ui)
ui
- the TableUI L&F objectUIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
JComponent.updateUI()
public String getUIClassID()
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
public void setModel(TableModel newModel)
newModel
- the new data source for this tablegetModel()
public TableModel getModel()
setModel(javax.swing.table.TableModel)
public void setColumnModel(TableColumnModel newModel)
newModel
- the new data source for this tablegetColumnModel()
public TableColumnModel getColumnModel()
setColumnModel(javax.swing.table.TableColumnModel)
public void setSelectionModel(ListSelectionModel newModel)
newModel
- the new selection modelgetSelectionModel()
public ListSelectionModel getSelectionModel()
setSelectionModel(javax.swing.ListSelectionModel)
public void tableChanged(TableModelEvent e)
public void columnAdded(TableColumnModelEvent e)
TableColumnModelListener
public void columnRemoved(TableColumnModelEvent e)
TableColumnModelListener
public void columnMoved(TableColumnModelEvent e)
TableColumnModelListener
public void columnMarginChanged(ChangeEvent e)
TableColumnModelListener
public void columnSelectionChanged(ListSelectionEvent e)
TableColumnModelListener
public void valueChanged(ListSelectionEvent e)
ListSelectionListener
public void editingStopped(ChangeEvent e)
CellEditorListener
public void editingCanceled(ChangeEvent e)
CellEditorListener
public void setPreferredScrollableViewportSize(Dimension size)
size
- a Dimension object specifying the preferredSize of a
JViewport whose view is this tableScrollable.getPreferredScrollableViewportSize()
public Dimension getPreferredScrollableViewportSize()
Scrollable.getPreferredScrollableViewportSize()
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
This method is called each time the user requests a unit scroll.
visibleRect
- The view area visible within the viewportorientation
- Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.direction
- Less than zero to scroll up/left, greater than zero for down/right.Scrollable.getScrollableUnitIncrement(java.awt.Rectangle, int, int)
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
Scrollable.getScrollableBlockIncrement(java.awt.Rectangle, int, int)
public boolean getScrollableTracksViewportWidth()
Scrollable.getScrollableTracksViewportWidth()
public boolean getScrollableTracksViewportHeight()
Scrollable.getScrollableTracksViewportHeight()
protected void createDefaultRenderers()
protected void createDefaultEditors()
protected void initializeLocalVars()
protected TableModel createDefaultDataModel()
protected TableColumnModel createDefaultColumnModel()
protected ListSelectionModel createDefaultSelectionModel()
protected JTableHeader createDefaultTableHeader()
protected void resizeAndRepaint()
public TableCellEditor getCellEditor()
cellEditor
public void setCellEditor(TableCellEditor anEditor)
anEditor
- the TableCellEditor that does the editingcellEditor
public void setEditingColumn(int aColumn)
editingColumn
public void setEditingRow(int aRow)
editingRow
public TableCellRenderer getCellRenderer(int row, int column)
row
- the row of the cell to render, where 0 is the firstcolumn
- the column of the cell to render, where 0 is the firstpublic Component prepareRenderer(TableCellRenderer renderer, int row, int column)
renderer
- the TableCellRenderer to preparerow
- the row of the cell to render, where 0 is the firstcolumn
- the column of the cell to render, where 0 is the firstpublic TableCellEditor getCellEditor(int row, int column)
row
- the row of the cell to edit, where 0 is the firstcolumn
- the column of the cell to edit, where 0 is the firstpublic Component prepareEditor(TableCellEditor editor, int row, int column)
editor
- the TableCellEditor to set uprow
- the row of the cell to edit, where 0 is the firstcolumn
- the column of the cell to edit, where 0 is the firstpublic void removeEditor()
protected String paramString()
null
.public boolean isManagingFocus()
public AccessibleContext getAccessibleContext()
|
JavaTM 2 Platform Standard Edition |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |