org.jdesktop.swingx
Class JXTable

java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by javax.swing.JComponent
extended by javax.swing.JTable
extended by org.jdesktop.swingx.JXTable
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CellEditorListener, ListSelectionListener, TableColumnModelListener, TableModelListener, Scrollable
Direct Known Subclasses:
JXTreeTable

public class JXTable
extends JTable


This is a partial copy of JXTable javadoc. The focus is on stand-alone table fields/methods (their details section), that is those which are unreleated to cross-component themes. All summaries and unrelated field/method details are cut.

Note: links inside this document are working, while links to other documents might not.

 

Field Detail

HORIZONTALSCROLL_ACTION_COMMAND

public static final String HORIZONTALSCROLL_ACTION_COMMAND
Identifier of show horizontal scroll action, used in JXTable's ActionMap.

See Also:
Constant Field Values

PACKALL_ACTION_COMMAND

public static final String PACKALL_ACTION_COMMAND
Identifier of pack table action, used in JXTable's ActionMap.

See Also:
Constant Field Values

PACKSELECTED_ACTION_COMMAND

public static final String PACKSELECTED_ACTION_COMMAND
Identifier of pack selected column action, used in JXTable's ActionMap.

See Also:
Constant Field Values

UIPREFIX

public static final String UIPREFIX
The prefix marker to find table related properties in the ResourceBundle.

See Also:
Constant Field Values



[...]



isXTableRowHeightSet

protected boolean isXTableRowHeightSet
Flag to distinguish internal settings of rowheight from client code settings. The rowHeight will be internally adjusted to font size on instantiation and in updateUI if the height has not been set explicitly by the application.

See Also:
adminSetRowHeight(int), setRowHeight(int)

[...]


Constructor Detail

JXTable

public JXTable()
Instantiates a JXTable with a default table model, no data.


JXTable

public JXTable(TableModel dm)
Instantiates a JXTable with a specific table model.

Parameters:
dm - The model to use.

JXTable

public JXTable(TableModel dm,
TableColumnModel cm)
Instantiates a JXTable with a specific table model.

Parameters:
dm - The model to use.

JXTable

public JXTable(TableModel dm,
TableColumnModel cm,
ListSelectionModel sm)
Instantiates a JXTable with a specific table model, column model, and selection model.

Parameters:
dm - The table model to use.
cm - The colomn model to use.
sm - The list selection model to use.

JXTable

public JXTable(int numRows,
int numColumns)
Instantiates a JXTable for a given number of columns and rows.

Parameters:
numRows - Count of rows to accomodate.
numColumns - Count of columns to accomodate.

JXTable

public JXTable(Vector rowData,
Vector columnNames)
Instantiates a JXTable with data in a vector or rows and column names.

Parameters:
rowData - Row data, as a Vector of Objects.
columnNames - Column names, as a Vector of Strings.

JXTable

public JXTable(Object[][] rowData,
Object[] columnNames)
Instantiates a JXTable with data in a array or rows and column names.

Parameters:
rowData - Row data, as a two-dimensional Array of Objects (by row, for column).
columnNames - Column names, as a Array of Strings.
Method Detail

[...]


packAll

public void packAll()
Resizes all columns to fit their content.

By default this method is bound to the pack all columns Action and registered in the table's ActionMap.

review-todo: add @see tags 
review-todo: move to column-focused?

packSelected

public void packSelected()
Resizes the lead column to fit its content.

By default this method is bound to the pack selected column Action and registered in the table's ActionMap.

review-todo: add @see tags 
review-todo: move to column-focused?

columnSelectionChanged

public void columnSelectionChanged(ListSelectionEvent e)
Invoked when the selection model of the TableColumnModel is changed.

Application code will not use these methods explicitly, they are used internally by JTable.

Overridden to update the enabled state of the pack selected column Action.

Specified by:
columnSelectionChanged in interface TableColumnModelListener
Overrides:
columnSelectionChanged in class JTable
Parameters:
e - the event received
See Also:
TableColumnModelListener
review-todo: move to column-focused?

setHorizontalScrollEnabled

public void setHorizontalScrollEnabled(boolean enabled)
Sets the enablement of enhanced horizontal scrolling. If enabled, it toggles an auto-resize mode which always fills the JViewport horizontally and shows the horizontal scrollbar if necessary.

The default value is false.

PENDING JW: the name is mis-leading?

Parameters:
enabled - a boolean indicating whether enhanced auto-resize mode is enabled.
See Also:
isHorizontalScrollEnabled()

isHorizontalScrollEnabled

protected boolean isHorizontalScrollEnabled()
Returns the current setting for horizontal scrolling.

Returns:
the enablement of enhanced horizontal scrolling.
See Also:
setHorizontalScrollEnabled(boolean)

setAutoResizeMode

public void setAutoResizeMode(int mode)
Sets the table's auto resize mode when the table is resized.

Overridden for internal bookkeeping related to the enhanced auto-resize behaviour.

Note: to enable/disable the enhanced auto-resize mode use exclusively setHorizontalScrollEnabled, this method can't cope with it.

Overrides:
setAutoResizeMode in class JTable
Parameters:
mode - One of 5 legal values: AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS, AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNS
See Also:
setHorizontalScrollEnabled(boolean)

updateHorizontalAction

protected void updateHorizontalAction()
Synchs selected state of horizontal scrolling Action to enablement of enhanced auto-resize behaviour.


getScrollableTracksViewportWidth

public boolean 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. Otherwise returns true.

Overridden to support enhanced auto-resize behaviour enabled and necessary.

Specified by:
getScrollableTracksViewportWidth in interface Scrollable
Overrides:
getScrollableTracksViewportWidth in class JTable
Returns:
false if autoResizeMode is set to AUTO_RESIZE_OFF, otherwise returns true
See Also:
setHorizontalScrollEnabled(boolean)

doLayout

public void doLayout()
Causes this table to lay out its rows and columns. Overridden so that columns can be resized to accomodate a change in the size of a containing parent. Resizes one or more of the columns in the table so that the total width of all of this JTable's columns is equal to the width of the table.

Overridden to support enhanced auto-resize behaviour enabled and necessary.

Overrides:
doLayout in class JTable
See Also:
setHorizontalScrollEnabled(boolean)
review-todo: remove inherit-doc, reference super instead


columnMarginChanged

public void columnMarginChanged(ChangeEvent e)
Invoked when a column is moved due to a margin change. If a cell is being edited, then editing is stopped and the cell is redrawn.

Application code will not use these methods explicitly, they are used internally by JTable.

Overridden to support enhanced auto-resize behaviour enabled and necessary.

Specified by:
columnMarginChanged in interface TableColumnModelListener
Overrides:
columnMarginChanged in class JTable
Parameters:
e - the event received
See Also:
setHorizontalScrollEnabled(boolean)

setFillsViewportHeight

public void setFillsViewportHeight(boolean fillsViewportHeight)
Sets the flag which controls the scrollableTracksViewportHeight property. If true the table's height will be always at least as large as the containing parent, if false the table's height will be independent of parent's height.

The default value is true.

Note: this a backport from Mustang's JTable.

Parameters:
fillsViewportHeight - boolean to indicate whether the table should always fill parent's height.
See Also:
getFillsViewportHeight(), getScrollableTracksViewportHeight()

getFillsViewportHeight

public boolean getFillsViewportHeight()
Returns the flag which controls the scrollableTracksViewportHeight property.

Returns:
true if the table's height will always be at least as large as the containing parent, false if it is independent
See Also:
setFillsViewportHeight(boolean), getScrollableTracksViewportHeight()

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Returns false to indicate that the height of the viewport does not determine the height of the table.

Overridden to control the tracksHeight property depending on fillsViewportHeight and relative size to containing parent.

Specified by:
getScrollableTracksViewportHeight in interface Scrollable
Overrides:
getScrollableTracksViewportHeight in class JTable
Returns:
true if the control flag is true and the containing parent height > prefHeight, else returns false.
See Also:
setFillsViewportHeight(boolean)




[...]


isDataChanged

protected boolean isDataChanged(TableModelEvent e)
Convenience method to detect dataChanged table event type.

Parameters:
e - the event to examine.
Returns:
true if the event is of type dataChanged, false else.

isUpdate

protected boolean isUpdate(TableModelEvent e)
Convenience method to detect update table event type.

Parameters:
e - the event to examine.
Returns:
true if the event is of type update and not dataChanged, false else.

isStructureChanged

protected boolean isStructureChanged(TableModelEvent e)
Convenience method to detect a structureChanged table event type.

Parameters:
e - the event to examine.
Returns:
true if the event is of type structureChanged or null, false else.

[...]


getVisibleRowCount

public int getVisibleRowCount()
Returns the preferred number of rows to show in a JScrollPane.

Returns:
the number of rows to show in a JScrollPane
See Also:
setVisibleRowCount(int)

setVisibleRowCount

public void setVisibleRowCount(int visibleRowCount)
Sets the preferred number of rows to show in a JScrollPane.

TODO JW - make bound property, reset scrollablePref(? distinguish internal from client code triggered like in rowheight?) and re-layout.

Parameters:
visibleRowCount - number of rows to show in a JScrollPane
See Also:
getVisibleRowCount()

getPreferredScrollableViewportSize

public Dimension getPreferredScrollableViewportSize()
Returns the preferred size of the viewport for this table.

TODO JW: refactor and comment.

Specified by:
getPreferredScrollableViewportSize in interface Scrollable
Overrides:
getPreferredScrollableViewportSize in class JTable
Returns:
a Dimension object containing the preferredSize of the JViewport which displays this table
See Also:
Scrollable.getPreferredScrollableViewportSize()

initializeColumnPreferredWidth

protected void initializeColumnPreferredWidth(TableColumn column)
Initialize the preferredWidth of the specified column based on the column's prototypeValue property. If the column is not an instance of TableColumnExt or prototypeValue is null then the preferredWidth is left unmodified.

TODO JW - need to cleanup getScrollablePreferred (refactor and inline) update doc - what exactly happens is left to the columnfactory.

Parameters:
column - TableColumn object representing view column
See Also:
TableColumnExt.setPrototypeValue(java.lang.Object)

scrollRowToVisible

public void scrollRowToVisible(int row)
Scrolls vertically to make the given row visible. This might not have any effect if the table isn't contained in a JViewport.

Note: this method has no precondition as it internally uses getCellRect which is lenient to off-range coordinates.

Parameters:
row - the view row index of the cell
See Also:
scrollColumnToVisible(int), scrollCellToVisible(int, int), JComponent.scrollRectToVisible(Rectangle)

scrollColumnToVisible

public void scrollColumnToVisible(int column)
Scrolls horizontally to make the given column visible. This might not have any effect if the table isn't contained in a JViewport.

Note: this method has no precondition as it internally uses getCellRect which is lenient to off-range coordinates.

Parameters:
column - the view column index of the cell
See Also:
scrollRowToVisible(int), scrollCellToVisible(int, int), JComponent.scrollRectToVisible(Rectangle)

scrollCellToVisible

public void scrollCellToVisible(int row,
int column)
Scrolls to make the cell at row and column visible. This might not have any effect if the table isn't contained in a JViewport.

Note: this method has no precondition as it internally uses getCellRect which is lenient to off-range coordinates.

Parameters:
row - the view row index of the cell
column - the view column index of the cell
See Also:
scrollColumnToVisible(int), scrollRowToVisible(int), JComponent.scrollRectToVisible(Rectangle)

getSelectionMode

public int getSelectionMode()
Returns the selection mode used by this table's selection model.

PENDING JW - setter?

Returns:
the selection mode used by this table's selection model
See Also:
ListSelectionModel.getSelectionMode()

[...]




getCellRenderer

public TableCellRenderer getCellRenderer(int row,
int column)
Returns an appropriate renderer for the cell specified by this row and column. If the TableColumn for this column has a non-null renderer, returns that. If not, finds the class of the data in this column (using getColumnClass) and returns the default renderer for this type of data.

Note: Throughout the table package, the internal implementations always use this method to provide renderers so that this default behavior can be safely overridden by a subclass.

Overridden to fix core bug #4614616 (NPE if TableModel's Class for the column is an interface). This method guarantees to always return a not null value. Returns the default renderer for Object if super returns null.

Overrides:
getCellRenderer in class JTable
Parameters:
row - the row of the cell to render, where 0 is the first row
column - the column of the cell to render, where 0 is the first column
Returns:
the assigned renderer; if null returns the default renderer for this type of object
See Also:
DefaultTableCellRenderer, TableColumn.setCellRenderer(javax.swing.table.TableCellRenderer), JTable.setDefaultRenderer(java.lang.Class, javax.swing.table.TableCellRenderer)

[...]


prepareEditor

public Component prepareEditor(TableCellEditor editor,
int row,
int column)
Prepares the editor by querying the data model for the value and selection state of the cell at row, column.

Note: Throughout the table package, the internal implementations always use this method to prepare editors so that this default behavior can be safely overridden by a subclass.

Overridden to adjust the editor's component orientation.

Overrides:
prepareEditor in class JTable
Parameters:
editor - the TableCellEditor to set up
row - the row of the cell to edit, where 0 is the first row
column - the column of the cell to edit, where 0 is the first column
Returns:
the Component being edited

adjustComponentOrientation

protected void adjustComponentOrientation(Component stamp)
Adjusts the Component's orientation to this JXTable's CO if appropriate. The parameter must not be null.

This implementation synchs the CO always.

Parameters:
stamp - the Component who's CO may need to be synched, must not be null.

getNewDefaultRenderer

public TableCellRenderer getNewDefaultRenderer(Class columnClass)
Returns a new instance of the default renderer for the specified class. This differs from getDefaultRenderer() in that it returns a new instance each time so that the renderer may be set and customized on a particular column.

Parameters:
columnClass - Class of value being rendered
Returns:
TableCellRenderer instance which renders values of the specified type
See Also:
JTable.getDefaultRenderer(Class)

createDefaultRenderers

protected void createDefaultRenderers()
Creates default cell renderers for objects, numbers, doubles, dates, booleans, and icons.

Overridden so we can act as factory for renderers plus hacking around huge memory consumption of UIDefaults (see #6345050 in core Bug parade)

Creates default cell renderers for objects, numbers, doubles, dates, booleans, and icons.

Overrides:
createDefaultRenderers in class JTable
See Also:
DefaultTableCellRenderer

createDefaultEditors

protected void createDefaultEditors()
Creates default cell editors for objects, numbers, and boolean values.

Overridden to hook enhanced editors plus hacking around huge memory consumption of UIDefaults (see #6345050 in core Bug parade)

Overrides:
createDefaultEditors in class JTable
See Also:
DefaultCellEditor

[...]



isTerminateEditOnFocusLost

public boolean isTerminateEditOnFocusLost()
Returns the property which determines the edit termination behaviour on focus lost.

Returns:
boolean to indicate whether an ongoing edit should be terminated if the focus is moved to somewhere outside of the table.
See Also:
setTerminateEditOnFocusLost(boolean)

setTerminateEditOnFocusLost

public void setTerminateEditOnFocusLost(boolean terminate)
Sets the property to determine whether an ongoing edit should be terminated if the focus is moved to somewhere outside of the table. If true, terminates the edit, does nothing otherwise. The exact behaviour is implemented in JTable.CellEditorRemover: "outside" is interpreted to be on a component which is not under the table hierarchy but inside the same toplevel window, "terminate" does so in any case, first tries to stop the edit, if that's unsuccessful it cancels the edit.

The default value is true.

Parameters:
terminate - the flag to determine whether or not to terminate the edit
See Also:
isTerminateEditOnFocusLost()

isAutoStartEditOnKeyStroke

public boolean isAutoStartEditOnKeyStroke()
Returns the autoStartsEdit property.

Returns:
boolean to indicate whether a keyStroke should try to start editing.
See Also:
setAutoStartEditOnKeyStroke(boolean)

setAutoStartEditOnKeyStroke

public void setAutoStartEditOnKeyStroke(boolean autoStart)
Sets the autoStartsEdit property. If true, keystrokes are passed-on to the cellEditor of the lead cell to let it decide whether to start an edit.

The default value is true.

Parameters:
autoStart - boolean to determine whether a keyStroke should try to start editing.
See Also:
isAutoStartEditOnKeyStroke()

updateUI

public void updateUI()
Notification from the UIManager that the L&F has changed. Replaces the current UI object with the latest version from the UIManager.

Additionally updates auto-adjusted row height and highlighters.

Another of the override motivation is to fix core issue (?? ID): super fails to update all renderers/editors.

Overrides:
updateUI in class JTable
See Also:
JComponent.updateUI()

updateHighlighterUI

protected void updateHighlighterUI()
Updates highlighter after updateUI changes.

See Also:
Highlighter.UIHighlighter

updateRowHeightUI

protected void updateRowHeightUI(boolean respectRowSetFlag)
Auto-adjusts rowHeight to something more pleasing then the default. This method is called after instantiation and after updating the UI. Does nothing if the given parameter is true and the rowHeight had been already set by client code. The underlying problem is that raw types can't implement UIResource.

This implementation asks the UIManager for a default value (stored with key "JXTable.rowHeight"). If none is available, calculates a "reasonable" height from the table's fontMetrics, assuming that most renderers/editors will have a border with top/bottom of 1.

Parameters:
respectRowSetFlag - a boolean to indicate whether client-code flag should be respected.
See Also:
isXTableRowHeightSet

setDefaultMargins

public void setDefaultMargins(boolean showHorizontalLines,
boolean showVerticalLines)
Deprecated. replaced by {@link #setShowGrid(boolean, boolean).

Convenience to set both grid line visibility and default margin for horizontal/vertical lines. The margin defaults to 1 or 0 if the grid lines are drawn or not drawn.

Parameters:
showHorizontalLines - boolean to decide whether to draw horizontal grid lines.
showVerticalLines - boolean to decide whether to draw vertical grid lines.

setShowGrid

public void setShowGrid(boolean showHorizontalLines,
boolean showVerticalLines)
Convenience to set both grid line visibility and default margin for horizontal/vertical lines. The margin defaults to 1 or 0 if the grid lines are drawn or not drawn.

Parameters:
showHorizontalLines - boolean to decide whether to draw horizontal grid lines.
showVerticalLines - boolean to decide whether to draw vertical grid lines.
See Also:
JTable.setShowGrid(boolean), JTable.setIntercellSpacing(Dimension)

setRowHeight

public void setRowHeight(int rowHeight)
Sets the height, in pixels, of all cells to rowHeight, revalidates, and repaints. The height of the cells will be equal to the row height minus the row margin.

Overriden to keep view/model coordinates of SizeSequence in synch. Marks the request as client-code induced.

Overrides:
setRowHeight in class JTable
Parameters:
rowHeight - new row height
See Also:
isXTableRowHeightSet

[...]


adminSetRowHeight

protected void adminSetRowHeight(int rowHeight)
Sets the rowHeight for all rows to the given value. Keeps the flag isXTableRowHeight unchanged. This enables the distinction between setting the height for internal reasons from doing so by client code.

Parameters:
rowHeight - new height in pixel.
See Also:
setRowHeight(int), isXTableRowHeightSet

rowAtPoint

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

Overridden to work around core Bug (ID #6291631): negative y is mapped to row 0).

Overrides:
rowAtPoint in class JTable
Parameters:
point - the location of interest
Returns:
the index of the row that point lies in, or -1 if the result is not in the range [0, getRowCount()-1]
See Also:
JTable.columnAtPoint(java.awt.Point)

createDefaultTableHeader

protected JTableHeader createDefaultTableHeader()
Returns the default table header object, which is a JTableHeader. A subclass can override this method to return a different table header object.

Overridden to return a JXTableHeader.

Overrides:
createDefaultTableHeader in class JTable
Returns:
the default table header object
See Also:
JXTableHeader



[...]