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, TableColumnModelExtListener
Direct Known Subclasses:
JXTreeTable

public class JXTable
extends JTable
implements TableColumnModelExtListener

A JXTable is a JTable with built-in support for row sorting, filtering, and highlighting, column visibility and a special popup control on the column header for quick access to table configuration. You can instantiate a JXTable just as you would a JTable, using a TableModel. However, a JXTable automatically wraps TableColumns inside a TableColumnExt instance. TableColumnExt supports visibility, sortability, and prototype values for column sizing, none of which are available in TableColumn. You can retrieve the TableColumnExt instance for a column using getColumnExt(Object) or getColumnExt(int colnumber).

A JXTable is, by default, sortable by clicking on column headers; each subsequent click on a header reverses the order of the sort, and a sort arrow icon is automatically drawn on the header. Sorting can be disabled using setSortable(boolean). Sorting on columns is handled by a Sorter instance which contains a Comparator used to compare values in two rows of a column. You can replace the Comparator for a given column by using getColumnExt("column").setComparator(customComparator)

Columns can be hidden or shown by setting the visible property on the TableColumnExt using TableColumnExt.setVisible(boolean). Columns can also be shown or hidden from the column control popup.

The column control popup is triggered by an icon drawn to the far right of the column headers, above the table's scrollbar (when installed in a JScrollPane). The popup allows the user to select which columns should be shown or hidden, as well as to pack columns and turn on horizontal scrolling. To show or hide the column control, use the setColumnControlVisible(boolean show)method.

Rows can be filtered from a JXTable using a Filter class and a FilterPipeline. One assigns a FilterPipeline to the table using setFilters(FilterPipeline). Filtering hides, but does not delete or permanently remove rows from a JXTable. Filters are used to provide sorting to the table--rows are not removed, but the table is made to believe rows in the model are in a sorted order.

One can automatically highlight certain rows in a JXTable by attaching Highlighters in the setHighlighters(HighlighterPipeline)method. An example would be a Highlighter that colors alternate rows in the table for readability; AlternateRowHighlighter does this. Again, like Filters, Highlighters can be chained together in a HighlighterPipeline to achieve more interesting effects.

You can resize all columns, selected columns, or a single column using the methods like packAll(). Packing combines several other aspects of a JXTable. If horizontal scrolling is enabled using setHorizontalScrollEnabled(boolean), then the scrollpane will allow the table to scroll right-left, and columns will be sized to their preferred size. To control the preferred sizing of a column, you can provide a prototype value for the column in the TableColumnExt using TableColumnExt.setPrototypeValue(Object). The prototype is used as an indicator of the preferred size of the column. This can be useful if some data in a given column is very long, but where the resize algorithm would normally not pick this up.

JXTable guarantees to delegate creation and configuration of TableColumnExt to a ColumnFactory. By default, the application-wide shared ColumnFactory is used. You can install a custom ColumnFactory, either application-wide by ColumnFactory.setInstance(ColumnFactory) or per table instance by setColumnFactory(ColumnFactory).

Last, you can also provide searches on a JXTable using the Searchable property.

Keys/Actions registered with this component:

Key bindings.

Client Properties.

See Also:
Serialized Form

Nested Class Summary
static class JXTable.BooleanEditor
          The default editor for Boolean types.
static class JXTable.BooleanRenderer
          The default renderer for Boolean types.
static class JXTable.DateRenderer
          The default renderer for Date types.
static class JXTable.DoubleRenderer
          Default renderer for Float and Double types.
static class JXTable.GenericEditor
          Default editor registered for Object.
static class JXTable.IconRenderer
          The default renderer for Icon and ImageIcon types.
static class JXTable.NumberEditor
          Editor for Numbers.
static class JXTable.NumberRenderer
          The default renderer for Number types.
protected static class JXTable.TableAdapter
           
static class JXTable.TableRolloverController<T extends JTable>
          listens to rollover properties.
 class JXTable.TableSearchable
           
 
Nested classes/interfaces inherited from class javax.swing.JTable
JTable.AccessibleJTable, JTable.PrintMode
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  ComponentAdapter dataAdapter
          The ComponentAdapter for model data access.
protected  FilterPipeline filters
          The FilterPipeline for the table.
protected  HighlighterPipeline highlighters
          The HighlighterPipeline for the table.
static String HORIZONTALSCROLL_ACTION_COMMAND
          Identifier of show horizontal scroll action, used in JXTable's ActionMap.
protected  boolean isXTableRowHeightSet
          Flag to distinguish internal settings of rowheight from client code settings.
static String MATCH_HIGHLIGHTER
          key for client property to use SearchHighlighter as match marker.
static String PACKALL_ACTION_COMMAND
          Identifier of pack table action, used in JXTable's ActionMap.
static String PACKSELECTED_ACTION_COMMAND
          Identifier of pack selected column action, used in JXTable's ActionMap.
protected  Highlighter resetDefaultTableCellRendererHighlighter
          The Highlighter used to hack around DefaultTableCellRenderer's color memory.
protected  Searchable searchable
          property to control search behaviour.
static String UIPREFIX
          The prefix marker to find table related properties in the ResourceBundle.
 
Fields inherited from class javax.swing.JTable
AUTO_RESIZE_ALL_COLUMNS, AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_OFF, AUTO_RESIZE_SUBSEQUENT_COLUMNS, autoCreateColumnsFromModel, autoResizeMode, cellEditor, cellSelectionEnabled, columnModel, dataModel, defaultEditorsByColumnClass, defaultRenderersByColumnClass, editingColumn, editingRow, editorComp, gridColor, preferredViewportSize, rowHeight, rowMargin, rowSelectionAllowed, selectionBackground, selectionForeground, selectionModel, showHorizontalLines, showVerticalLines, tableHeader
 
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
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JXTable()
          Instantiates a JXTable with a default table model, no data.
JXTable(int numRows, int numColumns)
          Instantiates a JXTable for a given number of columns and rows.
JXTable(Object[][] rowData, Object[] columnNames)
          Instantiates a JXTable with data in a array or rows and column names.
JXTable(TableModel dm)
          Instantiates a JXTable with a specific table model.
JXTable(TableModel dm, TableColumnModel cm)
          Instantiates a JXTable with a specific table model.
JXTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
          Instantiates a JXTable with a specific table model, column model, and selection model.
JXTable(Vector rowData, Vector columnNames)
          Instantiates a JXTable with data in a vector or rows and column names.
 
Method Summary
 void addHighlighter(Highlighter highlighter)
          Adds a Highlighter.
protected  void adjustComponentOrientation(Component stamp)
          Adjusts the Component's orientation to this JXTable's CO if appropriate.
protected  void adminSetRowHeight(int rowHeight)
          Sets the rowHeight for all rows to the given value.
 void columnMarginChanged(ChangeEvent e)
          Invoked when a column is moved due to a margin change.
 void columnPropertyChange(PropertyChangeEvent event)
          Notifies listeners about property changes of contained columns. Listens to column property changes.
 void columnRemoved(TableColumnModelEvent e)
          overridden to remove the interactive sorter if the sorted column is no longer contained in the ColumnModel.
 void columnSelectionChanged(ListSelectionEvent e)
          Invoked when the selection model of the TableColumnModel is changed.
protected  void configureColumnControl()
          Configures the upper trailing corner of an enclosing JScrollPane.
protected  void configureEnclosingScrollPane()
          Configures the enclosing JScrollPane.
 int convertRowIndexToModel(int row)
          Convert row index from view coordinates to model coordinates accounting for the presence of sorters and filters.
 int convertRowIndexToView(int row)
          Convert row index from model coordinates to view coordinates accounting for the presence of sorters and filters.
protected  JComponent createDefaultColumnControl()
          Creates the default column control used by this table.
protected  TableColumnModel createDefaultColumnModel()
          Returns the default column model object, which is a DefaultTableColumnModel.
 void createDefaultColumnsFromModel()
          Creates, configures and adds default TableColumns for columns in this table's TableModel.
protected  void createDefaultEditors()
          Creates default cell editors for objects, numbers, and boolean values.
protected  void createDefaultRenderers()
          Creates default cell renderers for objects, numbers, doubles, dates, booleans, and icons.
protected  JTableHeader createDefaultTableHeader()
          Returns the default table header object, which is a JTableHeader.
protected  ChangeListener createHighlighterChangeListener()
          Creates and returns the ChangeListener observing Highlighters.
protected  JXTable.TableRolloverController createLinkController()
           
protected  PipelineListener createPipelineListener()
          creates the listener for changes in filters.
protected  RolloverProducer createRolloverProducer()
          creates and returns the RolloverProducer to use.
 void doLayout()
          Layouts column width.
 TableCellRenderer getCellRenderer(int row, int column)
          Returns an appropriate renderer for the cell specified by this row and column.
 TableColumn getColumn(int viewColumnIndex)
          Returns the TableColumn at view position columnIndex.
 JComponent getColumnControl()
          Returns the component used as column control.
 int getColumnCount(boolean includeHidden)
          Returns the number of contained columns.
 TableColumnExt getColumnExt(int viewColumnIndex)
          Returns the TableColumnExt at view position columnIndex.
 TableColumnExt getColumnExt(Object identifier)
          Returns the first TableColumnExt with the given identifier.
 ColumnFactory getColumnFactory()
          Returns the ColumnFactory.
 int getColumnMargin()
          Returns the margin between columns.
 List<TableColumn> getColumns()
          Returns a List of visible TableColumns.
 List<TableColumn> getColumns(boolean includeHidden)
          Returns a List of contained TableColumns.
protected  ComponentAdapter getComponentAdapter()
           
protected  ComponentAdapter getComponentAdapter(int row, int column)
          Convenience to access a configured ComponentAdapter.
 boolean getFillsViewportHeight()
          Returns the flag which controls the scrollableTracksViewportHeight property.
protected  PipelineListener getFilterPipelineListener()
          returns the listener for changes in filters.
 FilterPipeline getFilters()
          Returns the FilterPipeline for the table.
protected  ChangeListener getHighlighterChangeListener()
          Returns the ChangeListener to use with highlighters.
 HighlighterPipeline getHighlighters()
          Returns the HighlighterPipeline assigned to the table, null if none.
protected  JXTable.TableRolloverController getLinkController()
           
 TableCellRenderer getNewDefaultRenderer(Class columnClass)
          Returns a new instance of the default renderer for the specified class.
 Dimension getPreferredScrollableViewportSize()
          Returns the preferred size of the viewport for this table.
 int getRowCount()
          Returns the row count in the table; if filters are applied, this is the filtered row count.
protected  SizeSequenceMapper getRowModelMapper()
          Returns the mapper used synch individual rowHeights in view/model coordinates.
 boolean getScrollableTracksViewportHeight()
          Returns false to indicate that the height of the viewport does not determine the height of the table.
 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.
 Searchable getSearchable()
           
 SelectionMapper getSelectionMapper()
           
 int getSelectionMode()
          Returns the selection mode used by this table's selection model.
protected  SortController getSortController()
          returns the currently active SortController.
 TableColumn getSortedColumn()
           
 SortOrder getSortOrder(int columnIndex)
          Returns the SortOrder of the given column.
 SortOrder getSortOrder(Object identifier)
          Returns the SortOrder of the given column.
protected  String getUIString(String key)
          Returns a potentially localized value from the UIManager.
 Object getValueAt(int row, int column)
          Overridden to account for row index mapping.
 int getVisibleRowCount()
          Returns the preferred number of rows to show in a JScrollPane.
protected  void initializeColumnPreferredWidth(TableColumn column)
          Initialize the preferredWidth of the specified column based on the column's prototypeValue property.
 boolean isAutoStartEditOnKeyStroke()
          Returns the autoStartsEdit property.
 boolean isCellEditable(int row, int column)
          Returns true if the cell at row and column is editable.
 boolean isColumnControlVisible()
          Returns the column control visible property.
protected  boolean isDataChanged(TableModelEvent e)
          Convenience method to detect dataChanged table event type.
 boolean isEditable()
          Returns the editable property of the JXTable as a whole.
 boolean isHorizontalScrollEnabled()
          Returns the current setting for horizontal scrolling.
 boolean isRolloverEnabled()
          Returns the rolloverEnabled property.
 boolean isRowHeightEnabled()
          Returns a boolean to indicate whether individual row height is enabled.
 boolean isSortable()
          Returns the table's sortable property.
protected  boolean isSortable(int columnIndex)
          Decides if the column at columnIndex can be interactively sorted.
protected  boolean isSortable(Object identifier)
          Decides if the column with identifier can be interactively sorted.
protected  boolean isStructureChanged(TableModelEvent e)
          Convenience method to detect a structureChanged table event type.
 boolean isTerminateEditOnFocusLost()
          Returns the property which determines the edit termination behaviour on focus lost.
protected  boolean isUpdate(TableModelEvent e)
          Convenience method to detect update table event type.
 void packAll()
          Resizes all columns to fit their content.
 void packColumn(int column, int margin)
          Packs an indivudal column in the table.
 void packColumn(int column, int margin, int max)
          Packs an indivual column in the table to less than or equal to the maximum witdth.
 void packSelected()
          Resizes the lead column to fit its content.
 void packTable(int margin)
          Packs all the columns to their optimal size.
 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.
 Component prepareRenderer(TableCellRenderer renderer, int row, int column)
          Returns the decorated Component used as a stamp to render the specified cell.
 void removeHighlighter(Highlighter highlighter)
          Removes the Highlighter.
protected  void resetDefaultTableCellRendererColors(Component renderer, int row, int column)
          Method to hack around #258-swingx: apply a specialized Highlighter to force reset the color "memory" of DefaultTableCellRenderer.
protected  void resetDefaultTableCellRendererColors(TableCellRenderer renderer, int row, int column)
          Method to hack around #258-swingx: apply a specialized Highlighter to force reset the color "memory" of DefaultTableCellRenderer.
 void resetSortOrder()
          Resets sorting of all columns.
 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].
 void scrollCellToVisible(int row, int column)
          Scrolls to make the cell at row and column visible.
 void scrollColumnToVisible(int column)
          Scrolls horizontally to make the given column visible.
 void scrollRowToVisible(int row)
          Scrolls vertically to make the given row visible.
 void setAutoResizeMode(int mode)
          Sets the table's auto resize mode when the table is resized.
 void setAutoStartEditOnKeyStroke(boolean autoStart)
          Sets the autoStartsEdit property.
 void setColumnControl(JComponent columnControl)
          Sets the component used as column control.
 void setColumnControlVisible(boolean visible)
          Sets the column control visible property.
 void setColumnFactory(ColumnFactory columnFactory)
          Sets the ColumnFactory to use for column creation and configuration.
 void setColumnMargin(int value)
          Sets the margin between columns.
 void setColumnSequence(Object[] identifiers)
          Reorders the columns in the sequence given array.
 void setComponentOrientation(ComponentOrientation o)
          Sets the language-sensitive orientation that is to be used to order the elements or text within this component.
 void setDefaultMargins(boolean showHorizontalLines, boolean showVerticalLines)
          Deprecated. replaced by setShowGrid(boolean, boolean).
 void setEditable(boolean editable)
          Sets the editable property.
 void setFillsViewportHeight(boolean fillsViewportHeight)
          Sets the flag which controls the scrollableTracksViewportHeight property.
 void setFilters(FilterPipeline pipeline)
          Sets the FilterPipeline for filtering table rows, maybe null to remove all previously applied filters.
 void setHighlighters(Highlighter... highlighters)
          Sets the Highlighters to the table, replacing any old settings.
 void setHighlighters(HighlighterPipeline pipeline)
          Assigns a HighlighterPipeline to the table, maybe null to remove all Highlighters.
 void setHorizontalScrollEnabled(boolean enabled)
          Sets the enablement of enhanced horizontal scrolling.
 void setLocale(Locale l)
          Sets the locale of this component.
 void setModel(TableModel newModel)
          Sets the data model for this table to newModel and registers with it for listener notifications from the new data model.
 void setRolloverEnabled(boolean rolloverEnabled)
          Property to enable/disable rollover support.
 void setRowHeight(int rowHeight)
          Sets the height, in pixels, of all cells to rowHeight, revalidates, and repaints.
 void setRowHeight(int row, int rowHeight)
          Sets the height for row to rowHeight, revalidates, and repaints.
 void setRowHeightEnabled(boolean enabled)
          Sets enablement of individual rowHeight support.
 void setSearchable(Searchable searchable)
          sets the Searchable for this editor.
 void setSelectionModel(ListSelectionModel newModel)
          Overridden to update selectionMapper
 void setShowGrid(boolean showHorizontalLines, boolean showVerticalLines)
          Convenience to set both grid line visibility and default margin for horizontal/vertical lines.
 void setSortable(boolean sortable)
          Sets "sortable" property indicating whether or not this table supports sortable columns.
 void setSortOrder(int columnIndex, SortOrder sortOrder)
          Sorts the table by the given column using SortOrder.
 void setSortOrder(Object identifier, SortOrder sortOrder)
          Sorts the table by the given column using the SortOrder.
 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.
 void setValueAt(Object aValue, int row, int column)
          Overridden to account for row index mapping.
 void setVisibleRowCount(int visibleRowCount)
          Sets the preferred number of rows to show in a JScrollPane.
 void tableChanged(TableModelEvent e)
          additionally updates filtered state.
 void toggleSortOrder(int columnIndex)
          Toggles the sort order of the column at columnIndex.
 void toggleSortOrder(Object identifier)
          Toggles the sort order of the column with identifier.
protected  void updateHighlighterUI()
          Updates highlighter after updateUI changes.
protected  void updateHorizontalAction()
          Synchs selected state of horizontal scrolling Action to enablement of enhanced auto-resize behaviour.
protected  void updateLocaleActionState(String key)
          Updates locale-dependent state of action registered with key in ActionMap.
protected  void updateLocaleState()
          Updates locale-dependent state.
protected  void updateOnFilterContentChanged()
          method called on change notification from filterpipeline.
protected  void updateRowHeightUI(boolean respectRowSetFlag)
          Auto-adjusts rowHeight to something more pleasing then the default.
 void updateUI()
          Notification from the UIManager that the L&F has changed.
protected  void updateViewSizeSequence()
          Called if individual row height mapping need to be updated.
 
Methods inherited from class javax.swing.JTable
addColumn, addColumnSelectionInterval, addNotify, addRowSelectionInterval, changeSelection, clearSelection, columnAdded, columnAtPoint, columnMoved, convertColumnIndexToModel, convertColumnIndexToView, createDefaultDataModel, createDefaultSelectionModel, createScrollPaneForTable, editCellAt, editCellAt, editingCanceled, editingStopped, getAccessibleContext, getAutoCreateColumnsFromModel, getAutoResizeMode, getCellEditor, getCellEditor, getCellRect, getCellSelectionEnabled, getColumn, getColumnClass, getColumnCount, getColumnModel, getColumnName, getColumnSelectionAllowed, getDefaultEditor, getDefaultRenderer, getDragEnabled, getEditingColumn, getEditingRow, getEditorComponent, getGridColor, getIntercellSpacing, getModel, getPrintable, getRowHeight, getRowHeight, getRowMargin, getRowSelectionAllowed, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedColumn, getSelectedColumnCount, getSelectedColumns, getSelectedRow, getSelectedRowCount, getSelectedRows, getSelectionBackground, getSelectionForeground, getSelectionModel, getShowHorizontalLines, getShowVerticalLines, getSurrendersFocusOnKeystroke, getTableHeader, getToolTipText, getUI, getUIClassID, initializeLocalVars, isCellSelected, isColumnSelected, isEditing, isRowSelected, moveColumn, paramString, print, print, print, print, processKeyBinding, removeColumn, removeColumnSelectionInterval, removeEditor, removeNotify, removeRowSelectionInterval, resizeAndRepaint, selectAll, setAutoCreateColumnsFromModel, setCellEditor, setCellSelectionEnabled, setColumnModel, setColumnSelectionAllowed, setColumnSelectionInterval, setDefaultEditor, setDefaultRenderer, setDragEnabled, setEditingColumn, setEditingRow, setGridColor, setIntercellSpacing, setPreferredScrollableViewportSize, setRowMargin, setRowSelectionAllowed, setRowSelectionInterval, setSelectionBackground, setSelectionForeground, setSelectionMode, setShowGrid, setShowHorizontalLines, setShowVerticalLines, setSurrendersFocusOnKeystroke, setTableHeader, setUI, sizeColumnsToFit, sizeColumnsToFit, unconfigureEnclosingScrollPane, valueChanged
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics,