Skip to main content

Default sorting criteria for JNTable/JXTable

3 replies [Last post]
karsten76
Offline
Joined: 2004-07-05
Points: 0

Is it possible to define a default sorting criteria (column) for JNTable or JXTable?

All methods I was able to find for doing this are private or protected :-(

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rameshgupta
Offline
Joined: 2004-06-04
Points: 0

> Is it possible to define a default sorting criteria
> (column) for JNTable or JXTable?
>
> All methods I was able to find for doing this are
> private or protected :-(

Yes, it is easy to set up a table with default sorters, specifying both the order in which columns should be sorted, as well as the direction in which to sort each column.

You can set up a JXTable to sort by column "foo" in descending order, followed by column "bar" in ascending order, and so on, simply by attaching a filter pipeline to the table. The filter pipeline should be constructed with a bunch of sorters (one for each column).

The sort direction (ascending or descending) can be specified on individual sorters. The order in which the sorters are added to the pipeline (at construction time) determine the order in which columns are sorted. When you use multiple sorters, the result is grouped sorting where subsequent columns are sorted within previous ones.

Of course, this only specifies the initial default sorting for the table. Users may interactively resort the table by clicking in column headers any way they want.

Here's the code:

JXTable table = new JXTable(model);
table.setFilters(new FilterPipeline(new Filter[] {
new ShuttleSorter(0, false), // column 0 descending
new ShuttleSorter(1, true), // column 1 ascending
}));

The column indices are specified in model coordinates. If necessary, the column index may be obtained by calling table.getModel().getColumnIndex(columnIdentifier); That's all there is to it!

Ramesh

jdubey
Offline
Joined: 2005-05-23
Points: 0

I am applying the technique above to set an initial sort order on a specific column in my JXTable. However, no sort arrow icon is displayed on initial view, even though the column is sorted. Is there a way to make this happen?

Thanks,

Josh

Kleopatra

jdnc-interest@javadesktop.org wrote:
> I am applying the technique above to set an
> initial sort order on a specific column in my JXTable.
> However, no sort arrow icon is displayed on initial
> view, even though the column is sorted.

The sort icon is shown if the "interactive" sorter is set - that's the
one the user can activate by clicking on the header. There's a
enhancement issue (in swingx #11) to add api for programmatically
set/reset the interactive sorter. Then doing so will show the arrow in
the header column.

On the other hand, if you are setting the "permanent" (wrong word - but
I don't know how else to put it) sorter(s) via the filterPipeline then
there is no sort indicator. There are no plans to add visual clues for
these - should there be? Any example in other applications?

Thanks
Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net