Rowsorting of TableView with SortedList/FilteredList

Martin Sladecek martin.sladecek at oracle.com
Fri Aug 16 05:50:19 PDT 2013


Hi,
there's JIRA issue tracking this: 
https://javafx-jira.kenai.com/browse/RT-32091

I was thinking about using FXCollections.sort() for this purpose, but I 
realized this is not going to work.
As FilteredList is an unmodifiable view of it's source list, the only 
thing you can sort the original list or a SortedList in the chain. The 
catch is that you don't know the element class of the original list nor 
the SortedList, so there's a problem with Comparator. Or even with the 
same type, the element could be theoretically filtered through a 
TransformationList that changes the element in a way their sort order 
would be different (like String -> String which does some translation).

Means only the developer (who created the FilteredList) is capable of 
sorting the FilteredList by placing a SortedList with appropriate 
Comparator below or on top of the FilteredList & binding the Comparator 
of the TableView to it.

Of course, TableView could do this itself, but as it was discussed 
before, this creates a problem of model-view indices, so it was decided 
that a developer must do the setup.

In other words, if you have any unmodifiable ObservableList / 
TransformationList, you can enable sorting just be wrapping it into a 
SortedList before passing it to the TableView.

Regards,
-Martin

On 08/16/2013 11:04 AM, Jonathan Giles wrote:
> This is a FilteredList issue that Martin Sladecek is looking into.
> -- Jonathan
> Sent from a touch device. Please excuse my brevity.
>
> "Martin Klähn" <grazertwo at gmail.com> wrote:
>> I've created https://javafx-jira.kenai.com/browse/RT-32391 for the
>> problem
>> that TableView is not sortable with a FilteredList
>>
>> - Martin
>>
>>
>> On Mon, Aug 12, 2013 at 1:14 PM, Martin Klähn <grazertwo at gmail.com>
>> wrote:
>>
>>> Sorry for the link I had that in the original mail but by adding it
>> seems
>>> to have vanished.
>>> So I had a chance to test the TableView with SortedList and
>> FilteredList
>>> with b102.
>>>
>>> Sorting is enabled with SortedList but not with FilteredList. I guess
>>> you'll want a issue for filtering with FilteredList?
>>>
>>> However I've run into a range of Exception in conjunction with
>> SortedLists
>>> created from FilteredList as item in TableView and user ordering of
>> Columns
>>> and user based changes of the FilteredList.predicate. They range from
>>> ArrayIndexOutOfBoundsException out of SortedList.java:313 to
>>> IndexOutOfBoundsException caused by TextInputControlBehavior.java:334
>> and
>>> NullPointerException in SortedList.java:247.
>>>
>>> I've built a small test class a user has to interact with. We've
>> searched
>>> for some automatic way of reproducing the error to no avail (up to
>> now).
>>> See https://www.dropbox.com/s/bfhqm0xk4y9r1oz/FilterSortedList.java
>>>
>>> Steps to reproduce:
>>> 1) change column sort of any column
>>> 2) type two characters in the textfield below which will change the
>>> FilterList.predicate based on String.startsWith-condition.
>>>
>>> Regards,
>>> Martin
>>>
>>>
>>>
>>> On Thu, Aug 8, 2013 at 10:30 PM, Jonathan Giles
>> <jonathan.giles at oracle.com
>>>> wrote:
>>>> Funny you should ask about this - I just blogged about SortedList
>> and
>>>> TableView the other day, over at FXExperience:
>>>> http://fxexperience.com/2013/**08/returning-a-tableview-back-**
>>>>
>> to-an-unsorted-state-in-**javafx-8-0<http://fxexperience.com/2013/08/returning-a-tableview-back-to-an-unsorted-state-in-javafx-8-0>
>>>> Of course, I can see that you've already read that post (I see a
>> comment
>>>> from the code in my post in your code below). What might have been
>> missed
>>>> is that I noted earlier on in the blog post I had to make a few
>> small
>>>> changes to properly get SortedList support in TableView, so you'll
>> want to
>>>> try again in b102 (or b103).
>>>>
>>>> Regarding your use of FilteredList as well - I've not tried this at
>> all,
>>>> but I'll add it to my todo list to investigate today. I imagine
>> there might
>>>> be a bug somewhere. Whatever I find will probably make for a good
>> post at
>>>> FXExperience, so keep an eye out there too.
>>>>
>>>> Thanks, and if you do run into further issues, please don't hesitate
>> to
>>>> file bugs. In general, if TableView isn't sorting then something is
>> going
>>>> really wrong!
>>>>
>>>> -- Jonathan
>>>>
>>>>
>>>> On 8/08/2013 11:17 p.m., Martin Klähn wrote:
>>>>
>>>>> Hi guys,
>>>>>
>>>>> I'm working on a business application that makes use of TableView
>> and I'm
>>>>> working with JDK 8 build b101.
>>>>>
>>>>> Displaying the data works like a charm. Row sorting for ordinary
>>>>> ObservableLists is fine too.
>>>>>
>>>>> Then I've set TableView.items to FilteredList and row sorting was
>>>>> disabled.
>>>>> replacing TableView.item with SortedList does not allow row sorting
>> as
>>>>> well. Binding the comparator of SortedList to the
>> TableView.comparator
>>>>> has
>>>>> no effect either.
>>>>>
>>>>>
>>>>> // row sorting possible
>>>>> //final TableView<Integer> tableView = new
>>>>> TableView<>(FXCollections.**observableArrayList(2, 1, 3));
>>>>>
>>>>> // row sorting not possible (SortedList)
>>>>> // create a TableView with the sorted list set as the items it will
>> show
>>>>> // bind the sortedList comparator to the TableView comparator
>>>>> //SortedList<Integer> sortedList = new
>>>>> SortedList<>(FXCollections.**observableArrayList(2, 1, 3));
>>>>> //sortedList.**comparatorProperty().bind(**
>>>>> tableView.comparatorProperty()**);
>>>>> //final TableView<Integer> tableView = new TableView<>(sortedList);
>>>>>
>>>>> // row sorting not possible (FilteredList)
>>>>> //FilteredList<Integer> filteredList = new
>>>>> FilteredList<>(FXCollections.**observableArrayList(2, 1, 3), (e) ->
>>>>> true);
>>>>> //final TableView<Integer> tableView = new TableView<>(filteredList
>> );
>>>>> // Don't forget to define columns!
>>>>> final TableColumn<Integer, Number> integerColumn = new
>>>>> TableColumn<>("Integer");
>>>>> final TableColumn<Integer, String> hexColumn = new
>> TableColumn<>("Integer
>>>>> Hex");
>>>>>
>>>>> integerColumn.**setCellValueFactory(javaClass -> new
>>>>> SimpleLongProperty(javaClass.**getValue()));
>>>>> hexColumn.setCellValueFactory(**javaClass -> new
>>>>>
>> SimpleStringProperty(Integer.**toHexString(javaClass.**getValue())));
>>>>> tableView.getColumns().addAll(**integerColumn, hexColumn);
>>>>>
>>>>>
>>>>> Any pointers on what I'm doing wrong or where I have to adapt my
>>>>> expectations.
>>>>> Is it correct that row sorting in a TableView is only possible for
>>>>> ordinary
>>>>> ObservableLists?
>>>>>
>>>>>
>>>>> With Regards
>>>>> Martin
>>>>>
>>>>



More information about the openjfx-dev mailing list