在 FXML 文件中设置自定义控件的 Button "onAction" 方法?

Set custom control's Button "onAction" method in the FXML file?

我创建了一个包含标签、文本字段和按钮的新自定义控件。如何在 FXML 文件中设置自定义控件的 Button "onAction" 方法?

示例代码:

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

只要 MyCustomComponent 包含名为 setOnButtonAction.

的事件处理程序的 setter,就可以使用

onButtonAction="#myCustomButtonAction"

class MyCustomComponent {

    private Button button;

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

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

    ...
}

好的,我找到了解决办法。我完全按照 Sergey Grinev 在本帖中的建议进行了操作 - JavaFX 2.0 - create action handler for custom component in FXML。我所做的唯一更改是在 Class 构造函数中使用按钮的字段名称而不是 "this"。