Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:16:57+00:00 2026-06-15T11:16:57+00:00

I’d like to implement a counter in my JavaFX application. The behavior is simply

  • 0

I’d like to implement a counter in my JavaFX application. The behavior is simply controlled via a Button:

  1. When the user clicks on the button first, then the countdown starts to countdown 10, 9, 8, …, 0
  2. When the user clicks on the button during the countdown, the countdown should canceled
  3. After 2. the user should be able to run the countdown again starting by 10

During the countdown, some heavy math computing is processed (on my application audio analysing).

Can you please look at my source code, if I doing it in the right way? In particular should I do the Platform.runLater stuff on the CountdownController or in the CountdownView and can I use a simple Java Thread or should I use the JavaFX Service/Task classes? Any suggestions are welcome.

The application is divided into 3 components:

  1. CountdownTest: creates the Stage and start JavaFX)
  2. CountdownView: singleton, contains a simple Button, passes button events to CountdownController
  3. CountdownController: singleton, starts a new Thread. Within the Thread the countdown is performed, the CountdownView is updatet to show the new countdown state and some math stuff is processed.

CountdownTest.java

package org.example;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class CountdownTest extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        final Group root = new Group();
        final Scene scene = new Scene(root);
        root.getChildren().setAll(CountdownView.getInstance());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        CountdownController.getInstance().stop();
    }

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

CountdownView.java

package org.example;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;

public final class CountdownView extends Group {
    private static final CountdownView instance = new CountdownView();

    private Button start;

    private CountdownView() {
        start = new Button("Start");
        start.setTooltip(new Tooltip("click to start countdown"));
        start.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                CountdownController.getInstance().onStartClick();
            }
        });

        this.getChildren().setAll(start);
    }

    public void startCountdown() {
        System.out.println("startCountdown");
        start.setTooltip(new Tooltip("click to stop countdown"));
    }

    public void setCountdown(final int countdown) {
        System.out.println("setCountdown " + countdown);
        start.setText(String.valueOf(countdown));
    }

    public void reset() {
        System.out.println("reset");
        start.setText("Start");
        start.setTooltip(new Tooltip("click to start countdown"));
    }

    public static CountdownView getInstance() {
        return instance;
    }
}

CountdownController.java

package org.example;

import javafx.application.Platform;

public final class CountdownController {
    private static final CountdownController instance = new CountdownController();

    private Thread countdownThread;
    private volatile boolean running = false;

    public void onStartClick() {
        if (!running) {
            countdownThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    running = true;
                    int countdown = 10;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            CountdownView.getInstance().startCountdown();
                            CountdownView.getInstance().setCountdown(10);
                        }
                    });
                    final long start = System.currentTimeMillis();
                    int lastCountdown = countdown;
                    while (!Thread.interrupted() && countdown > 0) {
                        countdown = (int) (10 - (System.currentTimeMillis() - start) / 1000);
                        if (countdown != lastCountdown) {
                            lastCountdown = countdown;
                            final int currentCountdown = countdown;
                            Platform.runLater(new Runnable() {
                                @Override
                                public void run() {
                                    CountdownView.getInstance().setCountdown(
                                            currentCountdown);
                                }
                            });
                        }

                        // Do some heavy computing stuff
                        for (int i = 0; i < 10000000; i++) {
                            Math.sin(Math.random());
                        }
                    }
                    running = false;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            CountdownView.getInstance().reset();
                        }
                    });
                }
            });
            countdownThread.start();
        } else {
            countdownThread.interrupt();
        }
    }

    public void stop() {
        if (countdownThread != null) {
            System.out.println("stop");
            countdownThread.interrupt();
        }
    }

    public static CountdownController getInstance() {
        return instance;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:16:58+00:00Added an answer on June 15, 2026 at 11:16 am

    Don’t over-sweat it. The task and service classes are just some utilities built over executors, if you don’t need what they have to offer then it’s just extra work.

    I think you are correct in using the controller to decide which thread things should be run on, your view has enough to do routing messages between the components and the controller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.