我创建了一个包含标签,文本字段和按钮的新自定义控件 . 如何在FXML文件中设置自定义控件的Button“onAction”方法?
示例代码:
<BorderPane fx:controller="fxmlexample.MyController"
xmlns:fx="http://javafx.com/fxml">
<top>
<MyCustomComponent onButtonAction="#myCustomButtonAction">
</MyCustomComponent>
</top>
2 回答
好的,我找到了解决方案 . 我完全按照谢尔盖格里涅夫在这个帖子中提出的那样做了 - JavaFX 2.0 - create action handler for custom component in FXML . 我做的唯一更改是在类构造函数中使用我的按钮的字段名称而不是"this" .
只要
MyCustomComponent
包含名为setOnButtonAction
的事件处理程序的setter,就可以使用onButtonAction="#myCustomButtonAction"
.