2017-08-11 74 views
1

如何在FXML - scenebuilder中調用接口默認方法。SceneBuilder中的JavaFX調用默認方法(FXML)

我有這樣一個接口:

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

,並像一個控制器:

BlaController implements Startable {...} 

但如果我調用該方法 「handleStart()」 中的FXML,我得到下面的異常:

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

是否有可能調用該方法?

+0

你的控制器可以實現'''javafx.fxml.Initializable''',並實現預期的結果,爲什麼你需要一個自定義接口? –

+0

無論如何,控制器正在實現Initializable以便能夠使用資源包,但是我看不出這應該如何幫助我? – Punika

回答

1

無法實現接口默認方法並在FXML中使用它,顯然FXMLLoader使用了反射並且在類實現中找不到方法。您必須重寫Controller類中的方法,然後調用默認方法。

界面保持不變。

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

這是你如何調用超級實施

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

希望它可以幫助...

+0

好吧,這似乎是fxml加載器的某種弱點。 但你的方式正常工作......感謝兄弟 – Punika