Nathan Howard Nathan is a Java/HTML5 developer. Nathan also enjoys writing technical blog-articles and playing games in his spare time.

Another Way to Listen for Object Changes in JavaFX

1 min read

At IDRSolutions we are currently in the process of creating a JavaFX implementation of our Swing PDF Viewer.

The Problem.

We recently encountered an issue when coding in a different View Mode (Page Flow). The issue was that we needed to only listen for when the user changes page and update other objects accordingly, however the class which has the object which changes the page already has a setOnMouseDragged Listener which performed a bunch of tasks and dragging the mouse on a slider is what changes the page. Having more than one listener on the same object was not an option as it was causing multiple conflicts and anomalies.

The Solution.

A quick solution to get around this was that we have a DoubleProperty object which has a method to set a double value, we used this method to set the page number. We could then tie a ChangeListener to the DoubleProperty object and listen for when the page number changed and perform whatever was needed accordingly.

The Example.

Normally to listen for some interaction with an object you can do something like :

Button btn = new Button("Go Forward a Page");
btn.setOnAction(new EventHandler() {
    public void handle(ActionEvent event) {

However if you do no want to use the setOnActionListener, you can also listen for a ChangeEvent on a double value like so :

DoubleProperty pageNoProperty = new SimpleDoubleProperty();
//User navigates forward a page, update page changer object.
pageNoProperty.addListener(new ChangeListener() {
    public void changed(ObservableValue o, Object oldVal, Object newVal) {

There are many ways to listen for a change in an object in JavaFX, this is just one of them, for more information on using Property Listeners, please visit : Properties and Binding page.

JavaFX is part of our Java PDF Library Viewer alongside the Swing iteration

Thank you for reading, I hope you enjoyed this article and will find it of use. If you have any questions, please post them in the comments section bellow.

You may find some of my JavaFX articles of interest :

We also have a JavaFX PDF Viewer plugin for NetBeans which you can grab here.

This post is part of our “JavaFX article Index” series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!

Are you a Developer working with PDF files?

Our developers guide contains a large number of technical posts to help you understand the PDF file Format.

Find out more about our software for Developers

Convert PDF to HTML5 or SVG Convert PDF to HTML5 or SVG
Convert AcroForms and XFA to HTML5Convert AcroForms and XFA to HTML5
Java PDF SDK for working with PDF files Java PDF SDK for working with PDF files
Nathan Howard Nathan is a Java/HTML5 developer. Nathan also enjoys writing technical blog-articles and playing games in his spare time.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2021. All rights reserved.