noteZ
noteZ

Reputation: 11

javafx progressbar bind color dynamically to progress

So i need to make a Timer for a ProgressIndicator/Bar. I did the 'animation' with a timeline, but I dont know how to change the color of the ProgressIndicator?

private ProgressIndicator progress = new ProgressIndicator();
private Timeline timeline = new Timeline();

public void doTime(int sec){

    Timeline time = new Timeline(

    new KeyFrame(
            Duration.ZERO,       
            new KeyValue(progress.progressProperty(), 1)
    ),
    new KeyFrame(
            Duration.seconds(sec), 
            new KeyValue(progress.progressProperty(), 0)
    )
);
time.setCycleCount(1);
time.play();
}

I would like to have the color red if the progress is at 0.33. I didnt find any listener for the progress.

if (progress.getProgress() <= 0.33) {
     progress.setStyle("-fx-progress-color: red;");

}

Do you have any ideas how to realize that?

Upvotes: 0

Views: 268

Answers (1)

James_D
James_D

Reputation: 209225

You can add a listener to the progress indicator's progressProperty():

progress.progressProperty().addListener((obs, oldProgress, newProgress) -> {
    if (newProgress <= 0.33) {
        progress.setStyle("-fx-progress-color: red;");

    } else {
        progress.setStyle("");
    }
});

Upvotes: 2

Related Questions