 |
| |
| META TOPICPARENT | name="JDNC" |
Home | Changes | Index | Search | Go <-- This creates the navigation links to : Home | Help | Index | etc. -->
SwingLabs: How to Use the SwingX JXTable | |
< < | A JXTable? is an extension to the standard Swing JTable adding support for runtime column selection, column rearrangement, background row highlighting, sorting and other features. A JXTable can be used just like a JTable, while allowing you to offer your users an extended feature set with no extra coding. | > > | A JXTable is an extension to the standard Swing JTable adding support for runtime column selection, background row highlighting, sorting and other features. A JXTable can be used just like a JTable, while allowing you to offer your users an extended feature set with no extra coding. | | | The extended features of JXTable are:
| Feature | What it does |
| Column Control | The JXTable introduces a small column control button, which opens a popup menu to configure the JXTable's display and behavior. The column control appears discretely to the right of the column headers. |
| |
< < |
| Selecting Columns | Users can select the columns they want to see on the JXTable by selecting the columns from the column control's popup menu. |
| Rearranging Columns | Users can change columns on screen by dragging and dropping the columns to their new position. |
| Highlighting Rows | You can highlight rows in a JXTable by attaching a Highlighter. Highlighting allows you to easily distinguish between different rows, for example, every other row, using a subtle background color scheme. |
| > > |
| Selecting Columns | Users can select the columns they want to see on the JXTable by selecting the columns from the column control's popup menu; this can also be done programmatically. |
| Highlighting Rows | You can highlight rows in a JXTable by attaching a Highlighter. Highlighting allows you to easily distinguish between different rows, for example, every other row, using a subtle background color scheme. |
| | |
| Sorting Rows | JXTable has built-in support for row sorting at runtime, allowing the user to click on a column header and sort by that column. Using custom Comparators, you can determine the sorting rules that apply to different columns. |
| Filtering Rows | You can attach one or more filters to a JXTable, allowing you to control which rows are shown to the user. |
| Resizing Columns | Users can auto-resize columns to fit their contents from the column control. |
| |
> > |
| Horizontal Scroll Control | With a single command, you can request that the table auto-resize to fit the viewport it's in, or to allow columns to scroll off to the right. |
| | | Contents of this How-To | | | This section covers how to use a JXTable in your code. We start with an overview of using a JXTable, then continue by describing each major feature of JXTable in turn. | |
> > | Sample Code
This How-To is based on a single sample program, org.jdesktop.demo.sample.SampleJXTableDemo in the SwingLabs-Demos subproject. Code samples shown here are taken from that class.
SampleJXTableDemo can be run directly from the command line as long as you have the SwingX distribution jar in your classpath. | | | Getting Started with a JXTable | |
< < | TODO: JXTable is a JTable, so you still need to code a TableModel? to use it, and wrap it in a JScrollPane? if there's enough data to scroll on. | > > | JXTable is a JTable subclass, and is instantiated in the same way--you need to provide a TableModel to use it, and wrap it in a JScrollPane so that headers show up.
From SampleJXTableDemo:
private JXTable initTable() {
// boilerplate table-setup; this would be the same for a JTable
SampleTableModel model = new SampleTableModel();
JXTable table = new JXTable(model);
model.loadData();
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return table;
}
| | | | |
> > | We won't show the SampleTableModel here--it's just an inner class subclassing DefaultTableModel that loads data from a text file. What's important is that we are just using a TableModel (or DefaultTableModel), and adding rows and columns just as we would with JTable.
Once we've initialized our JXTable, we can add it to a JScrollPane and we're ready to use it. | | |
The JXTable Column Control | |
< < | TODO: | > > | The JXTable Column Control is an icon that renders to the right of the column headers, just above the vertical scrollbar. When a user clicks the icon, a popup menu appears, allowing the user to show or hide columns from the view, and to set a couple of other properties.
The Column Control is available automatically as part of =JXTable=--that is, all you need to do is call
// All there is to it--users can now select which columns to view
jxTable.setColumnControlVisible(true);
And it shows up. Apart from showing and hiding columns, users can also change the horizontal scroll policy, and request that columns be resized (packed). Horizontal scrolling and column packing are described below. | | |
Selecting Columns at Runtime | |
< < | TODO: | > > | As we noted above, the column control on the table header allows users to show and hide columns in the table. The selection of columns is the set of all columns assigned to the TableModel. A column can be shown or hidden through the column control, as well as programmatically. In the SampleJXTableDemo, we hide some columns manually because our data source is too wide.
TableColumnExt latitude = jxTable.getColumnExt("LATITUDE");
latitude.setVisible(false); | | | | |
< < |
Rearranging Columns via Drag and Drop | > > | jxTable.getColumnExt("LONGITUDE").setVisible(false);
This sample code shows a long and a short way to do this. Visibility is a property of a TableColumnExt, a class which extends TableColumn. TableColumn itself doesn't know about visibility, whereas TableColumnExt allows a column to negotiate visibility with its owning table. Although you create a JXTable using a TableModel which contains TableColumns, a JXTable internally uses a TableColumnExt for each column. To get a reference to the TableColumnExt, you can use one of the accessor methods--=getColumnExt(Object identifier)= and getColumnExt(int viewColumnIndex). Once we have the TableColumnExt for a column, we simply use the setVisible(boolean showIt) method to show or hide the column. The Column Control uses this visibility mechanism in the background. | | | | |
< < | TODO: | | |
Highlighting Rows | |
< < | TODO: | > > | JXTable supports a highlighting mechanism which changes the background color of rows. With JTable, you would need to supply your own renderer to do this; with JXTable, you supply one or more Highlighters. A Highlighter implements rules for how a row's background should be colored.
In the SampleJXTableDemo, we use an AlternateRowHighlighter, a subclass of Highlighter with static constant Highlighter instances already configured for the task. The demo uses an AlternateRowHighlighter.classicLinePrinter to do the trick
jxTable.setHighlighters(new HighlighterPipeline(new Highlighter[]{ AlternateRowHighlighter.classicLinePrinter }));
An AlternateRowHighlighter changes the color of every other row in the table. The classicLinePrinter mimics the color scheme on old printer paper where every other line was colored a light green, to improve legibility.
A JXTable can have one or more Highlighters assigned in a HighlighterPipeline. The 'pipeline' concept is also used for Filters, and for sorting. A HighlighterPipeline is instantiated with an array of Highlighters, but you can also add and remove Highlighters from the HighlighterPipeline programmatically using addHighlighter() and removeHighlighter(). In our example, we use a single Highlighter passed in as an array to the HighlighterPipeline.
In the SampleJXTable demo, we add a second highlighter after the fact--this one tracks mouse movements and highlights the row on rollover:
// ...oops! we forgot one
jxTable.getHighlighters().addHighlighter(new RolloverHighlighter(Color.BLACK, Color.WHITE ));
jxTable.setRolloverEnabled(true);
When more than one Highlighter is in the HighlighterPipeline, each one is called in turn to apply its highlighting. You could add a ConditionalHighlighter on top of the AlternateHighlighter so that, apart from every other row being colored, certain columns that meet conditional criteria are automatically highlighted as well. Of course, you can also write your own Highlighters. | | |
Sorting Rows | |
< < | TODO: | > > | With JTable, programmers had to wrap the table in a TableSorter or other mechanism to provide automatic row sorting. JXTable has built-in support for sorting by column: users can sort any column by clicking on it. The column will sort alternately in ascending or descending order each time a given column is clicked.
JXTables have column sorting turned on by default. You can disable all column sorting using setSortingEnabled(boolean allowSort). You can also disable sorting on a single column by using setSorterClass(String sorterClassName) with a null sorter class name.
The default sorter used on a column can be overridden by supplying a new Comparator for the sorting algorithm. A good example of this is in the SwingXDemo, where a point (x, y) is sorted on the value of x and y as a Point, and not as a String. In the SimpleJXTableDemo, we create a special sorter for our columns with float values, to have the column sort in proper numeric order.
Comparator numberComparator = new Comparator() {
public int compare(Object o1, Object o2) {
Double d1 = Double.valueOf(o1 == null ? "0" : (String)o1);
Double d2 = Double.valueOf(o2 == null ? "0" : (String)o2);
return d1.compareTo(d2);
}
};
// First, the long way to assign--just to show there's a Sorter involved
Sorter sorter = jxTable.getColumnExt("ELEVATION").getSorter();
sorter.setComparator(numberComparator);
| | |
Filtering Rows from Display | |
< < | TODO: | > > | You can remove rows from a JXTable without modifying the underlying model by using Filters. Like Highlighters, Filters can be chained together in a pipeline, the FilterPipeline, and are added to a table using setFilters(FilterPipeline? pipeline).
In the SampleJXTableDemo, we use a filter to only show rows for countries who's names begin with A using a Pattern filter against the country column.
int col = jxTable.getColumn("COUNTRY").getModelIndex();
jxTable.setFilters(new FilterPipeline(new Filter[] { new PatternFilter("A.*", 0, col) }));
Filters don't actually remove or delete data from the TableModel; they just hide it (or, in the case of a Sorter, cause row order to appear different than the underlying model). Removing a PatternFilter, for example, will cause all rows that were previously hidden to be displayed. | | |
Resizing Columns | |
< < | TODO: | > > | JXTable supports several convenience methods for resizing column width. From the Column Control, to resize all columns to fit within the current viewport, select Pack All Columns. Programmatically, you can do this using the packAll(), packColumn(int col, int margin, int max), packSelected() and packTable(int margin) methods.
Packing will have a different effect if horizontal scrolling is enabled. With horizontal scrolling, the JXTable allows the table to scroll off to the right of the viewport, so the resize algorithm doesn't need to try and fit all columns within the more constrained viewport area. If you turn horizontal scrolling on, and pack all columns, you'll see them all get adjusted to their preferred width.
We should mention that the TableColumnExt also allows you to set prototype values. A prototype value for a column is a sample String that indicates the maximum length for a value in that column. Normally, when a column is resized, the JTable has to guess at the widest column value, as in large tables it couldn't actually check all values in the column. With the prototype value, you can hint to the table the size of data that will be in that column, allowing it to resize more accurately for the data set.
Horizontal Scrolling
As described above, normally a JXTable nestled in a JScrollPane will fit all columns within the scroll pane's viewport, possibly causing some of them to resize beneath an optimal width and making them illegible. You can use setHorizontalScrolling(boolean allowHoriz) with a value of true in order for the scroll pane to allow a horizontal scrollbar, with the end result that columns are resized to their preferred width. | | | Links to Demos and Related Documents | |
< < | TODO: swingX demo, other demos?
TODO: related classes: JXTreeTable?, JXScrollPane?, Filter?, Highlighter? | > > | TODO: SwingXDemo, other demos?
TODO: related classes: JXTreeTable?, JXScrollPane?, Filter?, Highlighter? | | | |
|
> > |
| META TOPICPARENT | name="JDNC" |
Home | Changes | Index | Search | Go <-- This creates the navigation links to : Home | Help | Index | etc. -->
SwingLabs: How to Use the SwingX JXTable
A JXTable? is an extension to the standard Swing JTable adding support for runtime column selection, column rearrangement, background row highlighting, sorting and other features. A JXTable can be used just like a JTable, while allowing you to offer your users an extended feature set with no extra coding.
The extended features of JXTable are:
| Feature | What it does |
| Column Control | The JXTable introduces a small column control button, which opens a popup menu to configure the JXTable's display and behavior. The column control appears discretely to the right of the column headers. |
| Selecting Columns | Users can select the columns they want to see on the JXTable by selecting the columns from the column control's popup menu. |
| Rearranging Columns | Users can change columns on screen by dragging and dropping the columns to their new position. |
| Highlighting Rows | You can highlight rows in a JXTable by attaching a Highlighter. Highlighting allows you to easily distinguish between different rows, for example, every other row, using a subtle background color scheme. |
| Sorting Rows | JXTable has built-in support for row sorting at runtime, allowing the user to click on a column header and sort by that column. Using custom Comparators, you can determine the sorting rules that apply to different columns. |
| Filtering Rows | You can attach one or more filters to a JXTable, allowing you to control which rows are shown to the user. |
| Resizing Columns | Users can auto-resize columns to fit their contents from the column control. |
Contents of this How-To
Working with a JXTable
This section covers how to use a JXTable in your code. We start with an overview of using a JXTable, then continue by describing each major feature of JXTable in turn.
Getting Started with a JXTable
TODO: JXTable is a JTable, so you still need to code a TableModel? to use it, and wrap it in a JScrollPane? if there's enough data to scroll on.
The JXTable Column Control
TODO:
Selecting Columns at Runtime
TODO:
Rearranging Columns via Drag and Drop
TODO:
Highlighting Rows
TODO:
Sorting Rows
TODO:
Filtering Rows from Display
TODO:
Resizing Columns
TODO:
Links to Demos and Related Documents
TODO: swingX demo, other demos?
TODO: related classes: JXTreeTable?, JXScrollPane?, Filter?, Highlighter? |
|