Java FX Digital Clock Example

In this tutorial we can see how to create digital clock with Java FX.

Here is the project structure:

First let’s create see the HelloApplication which has the main method:

Java

As you can see we extend Application and then provide implementation of start method. We use FXMLLoader to load hello-view.fxml which contains xml representation of the layout ant the components. The we create Scene and add it to the stage which is available in Application parent class.

Here is the hello-view.fxml:

XML

We also need to create controller class which load and initialize the components:

Java

In this case implement Initializable method initialize which will be called on time when the application is started. In side this method we set up the color of the background and the label text color and font size. We also create AnimationTimer instance implement method handle which will be called on every frame. Inside of this method we create LocalDateTime instance and we set it on the clockLabel which is the visible Java FX label. At the end we start the timer with timer.start().

The whole example you can find it in github

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.