How to make count down timer in JavaFx

preview_player
Показать описание
How to make count down timer in JavaFx. The video and audio doesnt match in the middle of the video due to recording frame rates i guess.I have pasted codes below for few who are looking for:
Рекомендации по теме
Комментарии
Автор

2 years later and still is very usefull, thanks a lot!
Greetings from Colombia.

andreschia
Автор

It is really useful even after 6 years. Thank you very much!

milo_radova
Автор

OMGGGG Thank you soooo much you are still helping people years later lol

elipeveto
Автор

Here is the code:

package application;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;


public class CountDownTimer extends Application {

Label lb;
Stage windows;
private final Integer starttime=15;
private Integer seconds= starttime;

@Override
public void start(Stage primaryStage) {
windows= primaryStage;
Group root= new Group();
lb= new Label();
lb.setText("Countdown: 15");
lb.setFont(Font.font(30));
lb.setTextFill(Color.RED);

doTime();

HBox layout= new HBox(5);
layout.getChildren().add(lb);

Scene scene= new Scene(root, 300, 70, Color.BLACK);
windows.setScene(scene);
windows.show();

}

private void doTime() {
Timeline time= new Timeline();


KeyFrame frame= new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent event) {


seconds--;
lb.setText("Countdown: "+seconds.toString());
if(seconds<=0){
time.stop();
}


}


});



if(time!=null){
time.stop();
}
time.play();


}

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


Hope, all enjoy the video, thanks!

paschalg
Автор

thank you very very much you are really helped me so much, you are perfect

TccSoft
Автор

Hey man, nice tutorial but I would keep it a bit shorter and some preperation would be good. I bet that way this video would have way more views.

ajobkustra
Автор

Thx for the video. It helped me a lot. ^^

jskauqwjksu
Автор

Ok this had some helpful information but overall very frustrating to watch: First of all please prepare in advance so you know what to write, and stop saying sorry all the time, it's really annoying and awkward.

me_too_thanks
Автор

Good video thanks. However, the code doesn't work seamlessly on all IDE's. I was using BlueJ and had to make a few minor changes. For example, I had to import the following class: import javafx.util.Duration in order for the Integer wrapper class object seconds to work. I also had to delete Group root = new Group() because HBox was already incorporated in the scene graph. I also had to delete root.getChildren().add(layout), which was irrelevant; Just so any future programmer doesn't have to painstakingly watch the video for 40 minutes and write each line of code here is my compiled version:

import
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.util.Duration;
import javafx.stage.Stage;

public class CountdownTimer extends Application{
private final Integer startTime = 10;
private Integer seconds = startTime;
private Label label;

@Override
public void start(Stage windows) throws Exception {
label = new Label();


HBox layout = new HBox(5);
layout.setLayoutX(20);


doTime();
windows.setScene(new Scene(layout, 300, 70, Color.BLACK));
windows.setTitle("Count down Timer");
windows.show();
}
private void doTime(){
//Duration duration = Duration.ofSeconds(10000);
Timeline time = new Timeline();

if(time!= null){//if the time is equal to zero it is finished but if it is not equal to something then it is //doing something
time.stop();
}
KeyFrame frame = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>(){
//every one second of the timeline the keyframe will do something (i.e., perform a job and that job //is defined by the event handler)
@Override
public void handle(ActionEvent event){
seconds--;


if(seconds <= 0){
time.stop();
Alert alert = new
alert.setHeaderText("Count down reset to 0!");
alert.show();
}
}
});

time.playFromStart();

}

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

Enjoy!

streetfashiontv
Автор

hi i loved ur video can you please send me the code i am trying to make a timer to a date for my project

jarrarjawhar
visit shbcf.ru