Ben Hayward
Ben Hayward

Reputation: 1512

Javafx Timeline Listener: ObservableValue to Double

I am trying to add a change listener to a the currentTimeProperty of a JavaFX Timeline. I would like to get the value of the current time, and have it represented as a double, so that I can perform operations on that value etc.

At the moment, this is what it looks like:

public void addAnimationListener()
{
    animation.getTimeline().currentTimeProperty().addListener(new ChangeListener(){
        @Override
        public void changed(ObservableValue arg0, Object arg1, Object arg2) {
            //double curPercentageValue = arg0.getValue(); //My attempt at trying to get the value to be a double. I tried casting it and such...
            System.out.println(arg0.getValue());
        }

    });
}

The values it prints out are e.g.:

128.33333333333334 ms

...So I could just perform String operations to remove the " ms" and then use Double.parseDouble() to get it to the desired data type...But is there a better way? One which gets the value directly?

Thanks in advance for your help!

Upvotes: 0

Views: 513

Answers (1)

James_D
James_D

Reputation: 209330

These things are always easier to figure out if you properly type your generic types (don't ignore compiler/IDE warnings):

public static void addAnimationListener()
{
    animation.getTimeline().currentTimeProperty().addListener(new ChangeListener<Duration>(){
        @Override
        public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
            double millis = newValue.toMillis();
            System.out.println(millis + " ms");
        }

    });
}

Upvotes: 1

Related Questions