2016-08-12 7 views
1

ラベル、テキストフィールド、およびボタンを含む新しいカスタムコントロールを作成しました。 FXMLファイルでカスタムコントロールのボタン "onAction"メソッドを設定するにはどうしたらいいですか?カスタムコントロールのボタン "onAction"メソッドをFXMLファイルに設定しますか?

例コード:

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

+0

ボタンを起動してFXMLで使用するカスタムコントロールからメソッドを公開する必要があります。 – ItachiUchiha

答えて

0

onButtonAction="#myCustomButtonAction"は限りMyCustomComponentsetOnButtonActionという名前のイベントハンドラのセッターが含まれているとして使用することができます。

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'がハンドラの型を取得するためにゲッターが必要であることを忘れてしまいました。今すぐ働かなければならない... – fabian

関連する問題