May 27, 2020

Update JavaFX GUI Periodically (e.g. every second)

If you want to periodically display new data in your GUI, then you could use "Timeline" (all classes from the javafx.*-packages), e.g. for updating every 1.0 second:


Timeline timeline = new Timeline(
    new KeyFrame(Duration.seconds(1.0), e -> {
        /*
         * read new data and update GUI nodes here ...
         */
    })
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();


If you use FXML, a good point to call this piece of code would be in your FXML controller class:


public class ControllerForFXML implements Initializable {

    @Override
    void initialize(URL location, ResourceBundle resources) {
        /*
         * Timeline code from above here ...
         */
    }
}

No comments:

Post a Comment