Java 8 SortedList TableView没有刷新 [英] Java 8 SortedList TableView not refreshing

查看:142
本文介绍了Java 8 SortedList TableView没有刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TableView来显示由ObservableList备份的SortedList。

I am using a TableView to visualize a SortedList backed up by an ObservableList.

SortedList的比较器绑定到TableView的比较器:

The comparator of the SortedList is bound to the comparator of the TableView:

sortedList().comparatorProperty().bind(tableView.comparatorProperty());

当我向项目添加项目时,SortedList按预期排序(通过所选TableView的比较器)底层ObservableList。

The SortedList sorts as expected (by the comparator of the TableView chosen) when I add an item to the underlying ObservableList.

虽然当我更改ObservableList的一个属性(带有CheckBoxTableCell,但这无关紧要)时,SortedList不会再次排序。

Although when I change a Property of the ObservableList (with a CheckBoxTableCell, but that shouldn't matter), the SortedList doesn't sort again.

这应该有效还是我必须找到解决方法?

Is this supposed to work or do I have to find a workaround?

我使用的是jdk 8 b121。

I am using jdk 8 b121.

推荐答案

注意:这个答案假定更改ObservableList的属性实际应该是更改项目的属性 ObservableList。假设是错误的,我会删除。

Note: this answer assumes that "change a property of the ObservableList" actually should read "change a property of an item of the ObservableList". It the assumption is incorrect, I'll delete.

SortedList是 在TableView中可排序数据的清洁解决方案 - 它的设计和实现在更改包装列表时保持自己排序。提供后,支持列表通知其侦听器有关更改。这是有效的,无需额外的客户端代码来修改列表(如添加/删除/设置项目)。

A SortedList is the clean solution for sortable data in a TableView - it's designed and implemented to keep itself sorted when the wrapped list is changed. Provided, the backing list notifies its listeners about the change. That's working without needing additional client code for modifications to the list (like add/remove/set of items).

另一方面,它没有一般的方法可以知道对包含项的属性的修改:客户端代码必须提供一个提取器(也就是返回数组的回调) of Observables)允许列表向其侦听器触发更新事件。

On the other hand, there is no general way it can know about modifications of properties of a contained item: client code must provide an extractor (aka callback that returns an array of Observables) that allows the list to fire update events to its listeners.

一个例子(人物是一个具有明显属性的演示bean - 用你最喜欢的例子bean替换),编辑按钮只是在z的前一个名称前面添加一个z选定的人。

An example (Person a demo bean with obvious properties - replace with your favourite example bean), the edit-button simply prepends a "z" to the last name of the selected person.

public class TableViewSortSample extends Application {

    private Parent getContent() {
        // wrap the backing list into an observableList with an extractor
        ObservableList<Person> persons = FXCollections.observableList(
                Person.persons(),
                person -> new Observable[] {person.lastNameProperty(), person.firstNameProperty()}            
        );
        // wrap the observableList into a sortedList
        SortedList<Person> sortedPersons = new SortedList<>(persons);
        // set the sorted list as items to a tableView
        TableView<Person> table = new TableView<>(sortedPersons);
        // bind the comparator of the sorted list to the table's comparator
        sortedPersons.comparatorProperty().bind(table.comparatorProperty());
        TableColumn<Person, String> firstName = new TableColumn<>("First Name");
        firstName.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        TableColumn<Person, String> lastName = new TableColumn<>("Last Name");
        lastName.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        table.getColumns().addAll(firstName, lastName);
        Button edit = new Button("Edit");
        edit.setOnAction(e -> {
            Person person = table.getSelectionModel().getSelectedItem();
            if (person != null) {
                person.setLastName("z" + person.getLastName());
            }
        });
        VBox pane = new VBox(table, edit);
        return pane;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(getContent()));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

这篇关于Java 8 SortedList TableView没有刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆