SceneBuilder中的JavaFX调用默认方法(FXML)

时间:2017-08-11 17:52:39

标签: javafx interface default fxml scenebuilder

如何在FXML中调用接口默认方法 - scenebuilder。

我有一个类似的界面:

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

和控制器如:

BlaController implements Startable {...}

但如果我在fxml中调用方法“handleStart()”,我会得到以下异常:

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

是否可以调用该方法?

2 个答案:

答案 0 :(得分:1)

无法实现接口默认方法并在FXML中使用它,显然FXMLLoader使用反射并且在类实现中找不到该方法。您必须覆盖Controller类中的方法,然后调用默认方法。

界面保持不变。

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

这是你可以调用超级实现的方法

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

希望它有所帮助...

答案 1 :(得分:0)

我创建了一个功能请求。也许将来会有一个直接解决这个问题的方法:https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8259916