Enhanced Table component with support for general SwingX sorting/filtering,
rendering, highlighting, rollover and search functionality. Table specific
enhancements include runtime configuration options like toggle column
visibility, column sizing, PENDING JW ...
Sorting and Filtering
JXTable supports sorting and filtering of rows.
Sorting support is single column only. It provides api to apply
a specific sort order or to toggle the sort order of columns identified
by view index or column identifier or reset all sorts.
table.setSortOrder("PERSON_ID", SortOrder.DESCENDING);
table.toggleSortOder(4);
table.resetSortOrder();
Sorting sequence can be configured per column by setting the TableColumnExt's
"comparator" property. Sorting can be disabled per column or per table by
#setSortable(boolean).
Typically, a JXTable is sortable by left clicking on column headers. By default, each
subsequent click on a header reverses the order of the sort, and a sort arrow
icon is automatically drawn on the header. The exact mapping of a user gesture to
a sort effect is configurable by installing a custom SortGestureRecognizer on the
JXTableHeader.
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 nor
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.
NOTE: SwingX sorting/filtering is incompatible with core sorting/filtering in
JDK 6+. Will be replaced by core functionality after switching the target jdk
version from 5 to 6.
Rendering and Highlighting
As all SwingX collection views, a JXTable is a HighlighterClient (PENDING JW:
formally define and implement, like in AbstractTestHighlighter), that is it
provides consistent api to add and remove Highlighters which can visually
decorate the rendering component.
An example multiple highlighting (default striping as appropriate for the
current LookAndFeel, cell foreground on matching pattern, and shading a
column):
Highlighter simpleStriping = HighlighterFactory.createSimpleStriping();
PatternPredicate patternPredicate = new PatternPredicate("ˆM", 1);
ColorHighlighter magenta = new ColorHighlighter(patternPredicate, null,
Color.MAGENTA, null, Color.MAGENTA);
Highlighter shading = new ShadingColorHighlighter(
new HighlightPredicate.ColumnHighlightPredicate(1));
table.setHighlighters(simpleStriping,
magenta,
shading);
To fully support, JXTable registers SwingX default table renderers instead of
core defaults (see
DefaultTableRenderer) The recommended approach for
customizing rendered content it to intall a DefaultTableRenderer configured
with a custom String- and/or IconValue. F.i. assuming the cell value is a
File and should be rendered by showing its name followed and date of last
change:
StringValue sv = new StringValue() {
public String getString(Object value) {
if (!(value instanceof File)) return StringValues.TO_STRING.getString(value);
return StringValues.FILE_NAME.getString(value) + ", "
+ StringValues.DATE_TO_STRING.getString(((File) value).lastModified());
}};
table.setCellRenderer(File.class, new DefaultTableRenderer(sv));
Note: DefaultTableCellRenderer and subclasses require a hack to play
nicely with Highlighters because it has an internal "color memory" in
setForeground/setBackground. The hack is applied by default which might lead
to unexpected side-effects in custom renderers subclassing DTCR. See
#resetDefaultTableCellRendererHighlighter for details.
Rollover
As all SwingX collection views, a JXTable supports per-cell rollover which is
enabled by default. If enabled, the component fires rollover events on
enter/exit of a cell which by default is promoted to the renderer if it
implements RolloverRenderer, that is simulates live behaviour. The rollover
events can be used by client code as well, f.i. to decorate the rollover row
using a Highlighter.
JXTable table = new JXTable();
table.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW,
null, Color.RED);
Search
As all SwingX collection views, a JXTable is searchable. A search action is
registered in its ActionMap under the key "find". The default behaviour is to
ask the SearchFactory to open a search component on this component. The
default keybinding is retrieved from the SearchFactory, typically ctrl-f (or
cmd-f for Mac). Client code can register custom actions and/or bindings as
appropriate.
JXTable provides api to vend a renderer-controlled String representation of
cell content. This allows the Searchable and Highlighters to use WYSIWYM
(What-You-See-Is-What-You-Match), that is pattern matching against the actual
string as seen by the user.
Column Configuration
JXTable's default column model
is of type TableColumnModelExt which allows management of hidden columns.
Furthermore, it guarantees to delegate creation and configuration of table columns
to its ColumnFactory. The factory is meant as the central place to
customize column configuration.
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.
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.
Keys/Actions registered with this component:
- "find" - open an appropriate search widget for searching cell content.
The default action registeres itself with the SearchFactory as search target.
- "print" - print the table
-
JXTable#HORIZONTALSCROLL_ACTION_COMMAND - toggle the horizontal
scrollbar
-
JXTable#PACKSELECTED_ACTION_COMMAND - resize the selected column
to fit the widest cell content
-
JXTable#PACKALL_ACTION_COMMAND - resize all columns to fit the
widest cell content in each column
Key bindings.
- "control F" - bound to actionKey "find".
Client Properties.
-
JXTable#MATCH_HIGHLIGHTER - set to Boolean.TRUE to use a
SearchHighlighter to mark a cell as matching.