In JavaFX, I discovered something strange (java version 1.8.0_91). As far as I understand, if you want to update the user interface from a separate stream, you need to either use Platform.runLater(taskThatUpdates)one of the tools in the package javafx.concurrent.
However, if I have TableViewwhich I am invoking .setItems(someObservableList), I can update someObservableListfrom a separate thread and see the corresponding changes in mine TableViewwithout the expected error Exception in thread "X" java.lang.IllegalStateException: Not on FX application thread; currentThread = X.
If replaced TableViewwith ListView, the expected error is expected.
Example code for the situation № 1: updating a TableViewanother thread, without having to call Platform.runLater()- and no errors.
public class Test extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
TableView<Integer> data = new TableView<>();
TableColumn<Integer, Integer> num = new TableColumn<>("Number");
num.setCellValueFactory(v -> new ReadOnlyObjectWrapper(v.getValue()));
data.getColumns().add(num);
VBox root = new VBox();
Scene scene = new Scene(root);
root.getChildren().addAll(data);
stage.setScene(scene);
stage.show();
ObservableList<Integer> someNumbers = FXCollections.observableArrayList();
data.setItems(someNumbers);
new Thread( () -> {
for (;;) {
try {
Thread.sleep(1000);
someNumbers.add((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
№ 2: a ListView , Platform.runLater() - .
public class Test extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ListView<Integer> data = new ListView<>();
VBox root = new VBox();
Scene scene = new Scene(root);
root.getChildren().addAll(data);
stage.setScene(scene);
stage.show();
ObservableList<Integer> someNumbers = FXCollections.observableArrayList();
data.setItems(someNumbers);
new Thread( () -> {
for (;;) {
try {
Thread.sleep(1000);
someNumbers.add((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
, data ListView<Integer>, a TableView<Integer>.
, ? - TableColumn::setCellValueFactory() ? - . , , , , , .setItems .