Search code examples
javafxinterfacedefaultfxmlscenebuilder

JavaFX call default method in SceneBuilder (FXML)


How can I call a interface default method in the FXML - scenebuilder.

I have an Interface like:

public interface Startable
{
   default void handleStart(){...}
}

and a controller like:

BlaController implements Startable {...}

but if I call the method "handleStart()" in the fxml, I get the following exception:

javafx.fxml.LoadException: Error resolving onMouseClicked='#handleStart', either the event handler is not in the Namespace or there is an error in the script.

Is there a possibility to call the method?


Solution

  • It is not possible to implement an interface default method and use it in FXML, apparently FXMLLoader uses reflection and doesn't find the method in the class implementation. You must override the method in the Controller class and then call the default method.

    The interface remains the same.

    public interface Startable {
        default void handleStart(){...}
    }
    

    This is how you can call the super implementation

    public class BlaController implements Startable {
        @Override
        @FXML
        void handleStart(){
            Startable.super.handleStart();
        }
    }
    

    Hope it helps...