Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Please ask technical questions in the appropriate category. Thank you!

Why does DoubleProperty implement Property<Number> and not Property<Double>?

cayhorstmannAug 31 2013 — edited Sep 2 2013

I am asking because it seems to be a bit inconvenient when implementing ChangeListeners.

For example,

slider.valueProperty().addListener((property, oldValue, newValue)
   -> message.setFont(new Font(newValue)));

does not work. I invite you find out the reason from the error message:

error: no suitable method found for addListener((property,[...]lue)))

      slider.valueProperty().addListener((property, oldValue, newValue)

                            ^

    method Observable.addListener(InvalidationListener) is not applicable

      (argument mismatch; incompatible parameter types in lambda expression)

    method ObservableValue.addListener(ChangeListener<? super Number>) is not applicable

      (argument mismatch; cannot type-check lambda expression with inferred parameter types

          inferred types: ObservableValue<? extends Number>,Number,Number)

That's actually bogus. newValue is inferred to be Number, and Number doesn't auto-unbox. This works:

slider.valueProperty().addListener((property, oldValue, newValue)
   -> message.setFont(new Font(newValue.doubleValue())));

So, that's all rather obscure. What was gained by having DoubleProperty implement Property<Number> and not Property<Double>?

Thanks,

Cay

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 30 2013
Added on Aug 31 2013
1 comment
17,941 views