延迟加载FXProperties

时间:2018-02-28 19:26:49

标签: java javafx tableview

这是here的后续行动。

我正在实现一个表,它将数据异步加载到表格单元格中。问题是,表格单元格有时无法正确更新。有时候它会以某种方式“挂起”并且显示“正在加载......”永远。只有当我在表格中稍微滚动时,实际值才会更新。

enter image description here

要重现,请在表格中快速向下滚动应用程序。有些单元格不会显示“延迟加载”值,而是占位符字符串。

延迟加载属性如下所示:

public abstract class LazyLoadingStringProperty extends SimpleStringProperty {

public static final String DEFAULT_LOADING_STRING = "Loading..";
private static final ExecutorService exe = Executors.newCachedThreadPool();
private String loadingString = DEFAULT_LOADING_STRING;

private boolean loaded = false;

public LazyLoadingStringProperty() {

}

public boolean isLoaded() {
    return loaded;
}

public void setLoaded(final boolean loaded) {
    this.loaded = loaded;
}

public String getLoadingString() {
    return loadingString;
}

public void setLoadingString(final String loadingString) {
    this.loadingString = loadingString;
}

@Override
public String getValue() {
    if (!loaded) {
        Platform.runLater(() -> startLoadingService());
        return loadingString;
    }
    return super.getValue();
}

protected void startLoadingService() {

    final Service<String> s = new Service<String>() {

        @Override
        protected Task<String> createTask() {
            return LazyLoadingStringProperty.this.createTask();
        }
    };

    s.setExecutor(exe);

    s.setOnFailed(e -> {
        setLoaded(true);
        setValue(s.getException().getLocalizedMessage());

    });

    s.setOnSucceeded(e -> {
        setLoaded(true);
        setValue(s.getValue());

    });
    s.start();
    // System.err.println("Started");
}

protected abstract Task<String> createTask();

}

Applikation看起来像这样:

public class ExampleTable extends Application {

    private static final int NUM_ELEMENTS = 500;

    private final TableView<ExampleBean> table = new TableView<>();

    private final ObservableList<ExampleBean> data = FXCollections.observableArrayList();

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

    @Override
    public void start(final Stage stage) {
        final Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(300);
        stage.setHeight(500);

        final TableColumn<ExampleBean, String> c1 = new TableColumn<>("A");
        c1.setCellValueFactory(new PropertyValueFactory<ExampleBean, String>("p1"));
        final TableColumn<ExampleBean, String> c2 = new TableColumn<>("B");
        c2.setCellValueFactory(new PropertyValueFactory<ExampleBean, String>("p2"));
        final TableColumn<ExampleBean, String> c3 = new TableColumn<>("C");
        c3.setCellValueFactory(new PropertyValueFactory<ExampleBean, String>("p3"));

        c1.setPrefWidth(100);
        c2.setPrefWidth(100);
        c3.setPrefWidth(100);


        for (int i = 0; i < NUM_ELEMENTS; i++) {
            data.add(new ExampleBean());
        }

        final ScrollPane sp = new ScrollPane();
        sp.setContent(table);
        sp.setMaxHeight(Double.POSITIVE_INFINITY);
        sp.setMaxWidth(Double.POSITIVE_INFINITY);
        sp.setFitToHeight(true);
        sp.setFitToWidth(true);

        table.setItems(data);
        // table.setMaxHeight(Double.POSITIVE_INFINITY);
        // table.setMaxWidth(Double.POSITIVE_INFINITY);
        table.getColumns().addAll(c1, c2, c3);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        VBox.setVgrow(sp, Priority.ALWAYS);
        vbox.getChildren().addAll(sp);

        scene.setRoot(vbox);


        stage.setScene(scene);
        stage.show();
    }
}

可以找到完整的可运行代码here

1 个答案:

答案 0 :(得分:0)

它看起来像是JavaFX服务中的一个错误...

由于没有保存对var options = RRule.parseString("FREQ=DAILY;INTERVAL=6"); options.dtstart = new Date(2018, 2, 1, 9, 30); options.until = new Date(2018, 3, 1, 9, 30); var rule = new RRule(options); rule.all(); // enumerate occurrences 个实例的引用,因此它们被垃圾收集 有时它们中的一些在任务完成之前就被垃圾收集了 在这种情况下,不会调用服务的ServiceonFailed属性的侦听器。

简单的解决方案是使用onSucceeded对象保存服务引用:

LazyLoadingStringProperty

但是没有必要并行加载一个包含许多任务的值。 因此,每个值的一项服务绰绰有余:

...
private ArrayList<Service<String>> services = new ArrayList<>();

protected void startLoadingService() {

    final Service<String> s = new Service<String>() {
        @Override
        protected Task<String> createTask() {
            return LazyLoadingStringProperty.this.createTask();
        }
    };
    services.add(s);
    ...