Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a timer to my program (javafx) [duplicate]

I am trying to add a timer to my program. I've tried calling upon java.util.Timer but it frustrates me as i do not completely understand the concepts behind it. I have just done a semester of coding in python but this is an entirely different level to me.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class main extends Application implements EventHandler<ActionEvent>{

    Button button;
    Button button2;
    Counter counter = new Counter(0);
    Counter timecounter = new Counter(0);
    Text text_counter = new Text(counter.S_count.get());
    Text text_timecounter = new Text(timecounter.S_count.get());

    Date currdate = new Date();
    int currsec = currdate.getSeconds();


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


    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Counter Window");

        button = new Button();
        button2 = new Button();
        button.setText("Reset");
        button.setOnAction(this);
        button2.setText("Tick");
        button2.setOnAction(this);
        button.setTranslateY(-120);
        button2.setTranslateY(-80);
        text_counter.textProperty().bind(counter.S_count);
        text_timecounter.textProperty().bind(timecounter.S_count);
        text_timecounter.setTranslateX(-100);



        StackPane layout = new StackPane();
        layout.getChildren().add(button);
        layout.getChildren().add(button2);
        layout.getChildren().add(text_counter);
        layout.getChildren().add(text_timecounter);

        Scene scene = new Scene(layout, 360,280);
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    @Override
    public void handle(ActionEvent event) {
        if(event.getSource()==button){
            counter.Reset();
        }
        if(event.getSource()==button2){
            counter.Tick();
        }
    }
}

I built a program in python with a main loop. I was thinking of a "dirty" way of doing it by adding a check to the loop that checks if the seconds of the current date has changed and if it has, increment a value by one.

class timerclass {
    Timer timer1;
    private int timecounter = 0;

    TimerTask Task1 = new TimerTask() {
        @Override
        public void run() {
            setTimecounter(getTimecounter()+1);
        }
    };

    public timerclass(){
        timer1 = new Timer();

    }

    public int getTimecounter() {
        return timecounter;
    }

    public void setTimecounter(int timecounter) {
        this.timecounter = timecounter;
    }
}

This is how far i've come creating a new timer. I understood that I have to create a task and schedule it to the timer but I don't have the slightest clue how...

How would I go about this? Sorry for the inconvieniences.

like image 246
Dennis Lukas Avatar asked Feb 19 '16 18:02

Dennis Lukas


People also ask

How do I create a countdown timer in JavaFX?

To start the timer, you click the Start Timer button. The numeric display then counts down—once per second—to zero. Anytime you click the Start Timer button, the timer resets to 15 and restarts the countdown. Listing 1 shows all of the code except the button's event handler code (lines 46-51), which we'll see later.

How do I add a delay in JavaFX?

The easiest way to delay a java program is by using Thread. sleep() method. The sleep() method is present in the Thread class. It simply pauses the current thread to sleep for a specific time.

Can a JavaFX application have more than one stage object?

This primary stage object creates a window to display user created functionality in the output. If your requirement is more than one stage then we can create as many Stage objects as we want. JavaFX Stage class available in javafx.

What is primaryStage in JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.


1 Answers

Timer is used to schedule tasks.. So where do you write those tasks?? Well you have to write those tasks in a TimerTask class...

Confused ? Let me break it down,

Timer timer = new Timer();

Now you have created a object of Timer class .. Now you have to do some task right? So to do that create an object of TimerTask.

TimerTask task = new TimerTask()
{
        public void run()
        {
            //The task you want to do       
        }

};

Now you have created a task where you should be defining the tasks you want to do inside the run method..

Why have I written a TimerTask class itself?? Thats because TimerTask is a abstract class with three methods.. So you need to define whichever method you want to use..

Now scheduling the task

timer.schedule(task,5000l);

Note the 'l' after 5000. It is to denote long data type because the schedule method is defined as

void schedule(TimerTask task,long milliseconds)

For further reference

https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html

https://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html

like image 112
Vaibhav G Avatar answered Sep 21 '22 09:09

Vaibhav G