Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application It's About Time Timeline Animation

Mojo Waters
Mojo Waters
5,748 Points

Animation is not running

Nope, no matter how hard I try, the clock (animation) just won't go down. It does eventually, because mTimeLine.setOnFinished(event -> { saveCurrentAttempt(); prepareAttempt(mCurrentAttempt.getKind() == AttemptKind.FOCUS ? AttemptKind.BREAK : AttemptKind.FOCUS); /* If currentAttempt is equal to FOCUS, switch to BREAK, if not, switch to FOCUS.

             */

    });
}

is working fine.

But it just won't show graphically.

Anybody knows what could be wrong?

package com.teamtreehouse.pomodoro.controllers;

import com.teamtreehouse.pomodoro.model.Attempt; import com.teamtreehouse.pomodoro.model.AttemptKind; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.layout.VBox; import javafx.util.Duration;

public class Home {

@FXML
private VBox container;

@FXML
private Label title;

@FXML
private TextArea message;

private Attempt mCurrentAttempt;
private StringProperty mTimerText;
private Timeline mTimeLine;


public Home() {
    mTimerText = new SimpleStringProperty();
    setTimerText(0);

}

public String getTimerText() {
    return mTimerText.get();
}

public StringProperty TimerTextProperty() {
    return mTimerText;
}

public void setTimerText(String TimerText) {
    mTimerText.set(TimerText);
}

public void setTimerText(int remainingSeconds) {
    int minutes = remainingSeconds / 60;
    int seconds = remainingSeconds % 60;
    setTimerText(String.format("%02d:%02d", minutes, seconds));

}

private void prepareAttempt(AttemptKind kind) {
    reset();
    mCurrentAttempt = new Attempt("", kind);
    addAttemptStyle(kind);
    title.setText(kind.getDisplayName());
    setTimerText(mCurrentAttempt.getRemainingSeconds());

    // This is creating multiple timelines.
    mTimeLine = new Timeline();
    mTimeLine.setCycleCount(kind.getTotalSeconds());
    mTimeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e -> {
        mCurrentAttempt.tick();
        setTimerText(mCurrentAttempt.getRemainingSeconds());
    }));


    mTimeLine.setOnFinished(event -> {
        saveCurrentAttempt();
        prepareAttempt(mCurrentAttempt.getKind() == AttemptKind.FOCUS ?
                AttemptKind.BREAK : AttemptKind.FOCUS);
            /*
            If currentAttempt is equal to FOCUS, switch to BREAK, if not, switch to FOCUS.

             */

    });
}

private void saveCurrentAttempt() {
        mCurrentAttempt.setMessage(message.getText());
        mCurrentAttempt.save();
    }

private void reset() {
    clearAttemptStyles();
    if (mTimeLine != null && mTimeLine.getStatus() == Animation.Status.RUNNING) {
        mTimeLine.stop();
    }
}

public void playTimer() {
    mTimeLine.play();
}

public void pauseTimer() {
    mTimeLine.pause();
}

private void addAttemptStyle(AttemptKind kind) {
    container.getStyleClass().add(kind.toString().toLowerCase());

}


private void clearAttemptStyles() {

    for (AttemptKind kind : AttemptKind.values()) {
        container.getStyleClass().remove(kind.toString().toLowerCase());

    }
}

public void handleRestart(ActionEvent actionEvent) {

    prepareAttempt(AttemptKind.FOCUS);
    playTimer();
}

}

package com.teamtreehouse.pomodoro.controllers;

import com.teamtreehouse.pomodoro.model.Attempt; import com.teamtreehouse.pomodoro.model.AttemptKind; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.layout.VBox; import javafx.util.Duration;

public class Home {

@FXML
private VBox container;

@FXML
private Label title;

@FXML
private TextArea message;

private Attempt mCurrentAttempt;
private StringProperty mTimerText;
private Timeline mTimeLine;


public Home() {
    mTimerText = new SimpleStringProperty();
    setTimerText(0);

}

public String getTimerText() {
    return mTimerText.get();
}

public StringProperty TimerTextProperty() {
    return mTimerText;
}

public void setTimerText(String TimerText) {
    mTimerText.set(TimerText);
}

public void setTimerText(int remainingSeconds) {
    int minutes = remainingSeconds / 60;
    int seconds = remainingSeconds % 60;
    setTimerText(String.format("%02d:%02d", minutes, seconds));

}

private void prepareAttempt(AttemptKind kind) {
    reset();
    mCurrentAttempt = new Attempt("", kind);
    addAttemptStyle(kind);
    title.setText(kind.getDisplayName());
    setTimerText(mCurrentAttempt.getRemainingSeconds());

    // This is creating multiple timelines.
    mTimeLine = new Timeline();
    mTimeLine.setCycleCount(kind.getTotalSeconds());
    mTimeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e -> {
        mCurrentAttempt.tick();
        setTimerText(mCurrentAttempt.getRemainingSeconds());
    }));


    mTimeLine.setOnFinished(event -> {
        saveCurrentAttempt();
        prepareAttempt(mCurrentAttempt.getKind() == AttemptKind.FOCUS ?
                AttemptKind.BREAK : AttemptKind.FOCUS);
            /*
            If currentAttempt is equal to FOCUS, switch to BREAK, if not, switch to FOCUS.

             */

    });
}

private void saveCurrentAttempt() {
        mCurrentAttempt.setMessage(message.getText());
        mCurrentAttempt.save();
    }

private void reset() {
    clearAttemptStyles();
    if (mTimeLine != null && mTimeLine.getStatus() == Animation.Status.RUNNING) {
        mTimeLine.stop();
    }
}

public void playTimer() {
    mTimeLine.play();
}

public void pauseTimer() {
    mTimeLine.pause();
}

private void addAttemptStyle(AttemptKind kind) {
    container.getStyleClass().add(kind.toString().toLowerCase());

}


private void clearAttemptStyles() {

    for (AttemptKind kind : AttemptKind.values()) {
        container.getStyleClass().remove(kind.toString().toLowerCase());

    }
}

public void handleRestart(ActionEvent actionEvent) {

    prepareAttempt(AttemptKind.FOCUS);
    playTimer();
}

}

2 Answers

Nishay Madhani
Nishay Madhani
15,456 Points

public StringProperty timerTextProperty() { return mTimerText; }

Paste this code in your Home.java

Nishay Madhani
Nishay Madhani
15,456 Points

Even i have the same problem Somebody please help