This is a sequel here .
I am implementing a table that asynchronously loads data into table cells. The problem is that table cells are sometimes not updated correctly. Sometimes it somehow “hangs”, but shows “Loading ..” forever . The actual value is updated only when I scroll the table a bit.

To play , run the application by quickly scrolling through the table. Some cells do not display a “lazily loaded” value, but a placeholder string.
The lazy-load property looks like this:
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();
}
protected abstract Task<String> createTask();
}
The application is as follows:
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.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();
}
}
The full startup code can be found here .