Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx timeline - set initial delay

Tags:

java

javafx-2

I recently started to use JavaFx 2.0 and maybe my problem is very basic, but currently I have no idea how to solve it. For example, let's say I have this small demo application called Clock:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;

import java.util.Date;

public class ClockDemo extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        final Label label = new Label(new Date().toString());
        label.setFont(new Font("Arial", 18));
        final Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(5), new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                label.setText(new Date().toString());
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);

        Button button = new Button("Start");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                timeline.play();
            }
        });

        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                timeline.stop();
            }
        });
        HBox hBox = HBoxBuilder.create()
                .spacing(5.0)
                .padding(new Insets(5, 5, 5, 5))
                .children(label, button)
                .build();

        Scene scene = new Scene(hBox, 330, 30);
        stage.setScene(scene);
        stage.setTitle("Clock demo");
        stage.show();
    }
}

Basically, if you click start button, Timeline will update time in Label every 5 seconds. But problem I'm facing is that when I click start button I have to wait for 5 seconds until Timeline starts to run and updates time in Label. So, is there a any way to eliminate this initial delay time?

like image 469
Branislav Lazic Avatar asked Aug 18 '13 22:08

Branislav Lazic


1 Answers

I had a same problem and I solved it by adding a KeyFrame with the Duration.ZERO at the beginning of timeline and add my action to it and the second KeyFrame is responsible for delay.

final Timeline timeline = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    label.setText(new Date().toString());
                }
            }) , new KeyFrame(Duration.seconds(5)));
like image 178
Eeliya Avatar answered Sep 21 '22 00:09

Eeliya