1

I am trying to add a timer in my screen recording application being developed using JavaFX (IDE: NetBeans) and OS (Win 10). I am adding a timer now, which is to be used as:

User clicks on start recording, A Label on my application should start showing the time as 00:00:00, I have the time in seconds which is being printed on console even but when I use it to be displayed on application it just shows 0 Seconds. I am attaching code too. Kindly help. How can I refresh app interface or run timer when I have the value, It is just not updating.

Here you can see my app doesn't start timer; however, is being printed on consoleApp's Snap Console Snap

    private void scheduleTimerTasks() {
        isRecording = true;
        int delay =50; 
        long period = 500;
                //1000 / 48;
        RecordTimer.reset();
        timerRecord = new Timer("Thread TimerRecord");
        timerCount = new Timer("Thread TimerCount");
        recorderTask = new ScreenRecorderTask(encoder, rectangle);
        countTask = new TimerCountTask(Timer);
        timerRecord.scheduleAtFixedRate(recorderTask, delay, period);
        timerCount.scheduleAtFixedRate(countTask, delay, period);
        Timer.setText(""+countTask.timeInSec+" s"); //Setting Label Text
        System.out.println(countTask.timeInSec);
        recordStateLabel.setText("recorder Started...");
    }
Atif Aziz
  • 31
  • 6
  • 1
    lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions) got this – Atif Aziz Jul 23 '19 at 14:18
  • Use Java 8 then? Instead of 7... – trilogy Jul 23 '19 at 14:19
  • It is required, otherwise, I would have – Atif Aziz Jul 23 '19 at 14:20
  • Ok do it like this: `Platform.runLater(new Runnable() { @Override public void run() { timer.setText(""+countTask.timeInSec+" s"); } });` – trilogy Jul 23 '19 at 14:20
  • What is Platform? I need to import something or what? Its giving error on Platform only. – Atif Aziz Jul 23 '19 at 14:22
  • Aren't you using NetBeans? It should auto import `import javafx.application.Platform;` – trilogy Jul 23 '19 at 14:22
  • Its still showing 0 s, however, I just checked my java version, and its 8. Idk why that error came. Apologies. – Atif Aziz Jul 23 '19 at 14:31
  • Possible duplicate of [How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?](https://stackoverflow.com/questions/21083945/how-to-avoid-not-on-fx-application-thread-currentthread-javafx-application-th) – trilogy Jul 23 '19 at 14:58

1 Answers1

1

You could use AnimationTimer from javafx.animation.animationTimer, the handle' method of this timer will be called every frame.

Daniel Jeney
  • 486
  • 1
  • 5
  • 19
  • It has started running but can you tell me why it is 2x of the actual time? Where can I set its FPS or whatever? If I record for 8 seconds, it is at 17 seconds – Atif Aziz Jul 23 '19 at 15:09
  • @AtifAziz The FPS shouldn't matter. The single argument of the `AniamtionTimer#handle(long)` method is the timestamp (in nanoseconds) of the frame. If you keep track of the previous timestamp and subtract it from the current timestamp you'll have the time elapsed (in nanoseconds) between the two frames. – Slaw Jul 23 '19 at 21:53