[英]JavaFX8 - FXML How to call method with parameters in onAction-tag?
How is it possible to call a method with parameters out of FXML? 如何用FXML中的参数调用方法?
In Java I use this code: 在Java中我使用以下代码:
textfield.setOnAction(event -> {
endEdit(false);
});
In FXML I know I can call a method like this: 在FXML中,我知道我可以调用这样的方法:
<TextField onAction="#endEdit">
So how can I call the method endEdit(Boolean)
in FXML with the parameter false
? 那么如何用参数
false
调用FXML中的方法endEdit(Boolean)
?
You could just encapsulate the endEdit(...)
method call in a @FXML
annotated method that handles the action event. 您可以将
endEdit(...)
方法调用封装在处理动作事件的@FXML
注释方法中。 Something like this: 像这样的东西:
public class FXMLController implements Initializable {
@FXML
protected void handleTextFieldAction(ActionEvent e) {
endEdit(false);
}
private void endEdit(boolean flag) {
System.out.println("Flag value: " + flag);
// Your implementation here
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Then in your FXML
file bind the text field's onAction property to this handleTextFieldAction(...)
method like this: 然后在你的
FXML
文件中将文本字段的onAction属性绑定到此handleTextFieldAction(...)
方法,如下所示:
<TextField onAction="#handleTextFieldAction" />
If the boolean flag actually depends on some conditions that have to be evaluated then you can process them within handleTextFieldAction(...)
method and call endEdit(...)
with the appropriate value. 如果布尔标志实际上取决于某些必须评估的条件,那么您可以在
handleTextFieldAction(...)
方法中处理它们并使用适当的值调用endEdit(...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.