[SOLVED] Compiler error: overloaded method [..] with alternatives

Given the following code:

import javafx.beans.property.{BooleanProperty, SimpleBooleanProperty}
import javafx.beans.value.{ChangeListener, ObservableValue}

val booleanProperty: BooleanProperty = new SimpleBooleanProperty()
booleanProperty.addListener((_, _, newValue) => {})
booleanProperty.addListener(new ValueChangeListener)

final class ValueChangeListener extends ChangeListener[Boolean] {
  def changed(observable: ObservableValue[_ <: Boolean], oldValue: Boolean, newValue: Boolean): Unit = ???

In IntelliJ this leads to the following compiler error:

overloaded method addListener with alternatives: (x$1: javafx.beans.value.ChangeListener[_ >: java.lang.Boolean])Unit <and> (x$1: javafx.beans.InvalidationListener)Unit cannot be applied to (com.example.app.ApplicationContext.Preferences.PreferencesValueChangeListener[scala.Boolean]) hidePackageColumnProperty.addListener(new PreferencesValueChangeListener(hidePackageColumnSpec))

But why? The first addListener works fine, the second does not.


scala.Boolean does not match the expected java.lang.Boolean.

import java.lang.{ Boolean => JBoolean }

final class ValueChangeListener extends ChangeListener[JBoolean] {
  def changed(observable: ObservableValue[_ <: JBoolean], oldValue: JBoolean, newValue: JBoolean): Unit = ???
1 Like

Thank you, I did know that but it’s a hard one to spot if it happens.