2016-08-12 31 views
1

我創建了一個新的自定義控件,其中包含標籤,文本字段和一個按鈕。如何在FXML文件中設置自定義控件的Button「onAction」方法?在FXML文件中設置自定義控件的按鈕「onAction」方法?

示例代碼:

<BorderPane fx:controller="fxmlexample.MyController" 
xmlns:fx="http://javafx.com/fxml"> 
<top> 
    <MyCustomComponent onButtonAction="#myCustomButtonAction"> 
    </MyCustomComponent> 
</top> 

+0

您需要從您的自定義控件暴露一個方法,該方法觸發按鈕,然後在FXML中使用它。 – ItachiUchiha

回答

0

onButtonAction="#myCustomButtonAction"可以只要MyCustomComponent包含名爲setOnButtonAction事件處理程序的設定器使用。

class MyCustomComponent { 

    private Button button; 

    public void setOnButtonAction(EventHandler<ActionEvent> handler) { 
     this.button.setOnAction(handler); 
    } 

    public EventHandler<ActionEvent> getOnButtonAction() { 
     return this.button.getOnAction(); 
    } 

    ... 
} 
+0

不幸的是,這沒有奏效。但我找到了解決方案(在我的答案中)。 – notmyf4ulty

+0

@ notmyf4ulty抱歉,忘記了'FXMLLoader'需要getter來獲取處理程序類型。現在應該工作... – fabian