[英]JavaFX FXML RadioButton binding
How to bind a radio button to a model class? 如何将单选按钮绑定到模型类?
<fx:define>
<ToggleGroup fx:id="xxx" />
</fx:define>
<children>
<RadioButton text="one" toggleGroup="$xxx" fx:id="f1"/>
<RadioButton text="two" toggleGroup="$xxx" fx:id="f2"/>
</children>
` `
model: 模型:
private final StringProperty yyy = new SimpleStringProperty(this, "yyy", "");
with getters and setters 与吸气剂和二传手
i tried something like this in controller 我在控制器中尝试过这样的事情
@FXML
private String yyy = "";
@FXML private RadioButton f1;
@FXML private RadioButton f2;
and then I can get the 然后我可以得到
if (f1.isSelected())
{yyy = f1.getText();}
and store the value of radio in String yyy, but how to send it to model from there.. 并将radio的值存储在String yyy中,但是如何从那里将其发送到模型中。
I didn't quite understand where you want to send what from where to where, but may be you can (bidirectionally? Unidirectionally?) bind yyy
to RadioButton.textProperty()
? 我不太了解您要从哪里发送什么到什么地方,但是也许可以(双向?单向?)将
yyy
绑定到RadioButton.textProperty()
吗? Or maybe just use the RadioButton.setText(String)
method. 或者也许只是使用
RadioButton.setText(String)
方法。
Have a look at here https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html#textProperty-- 在这里看看https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html#textProperty--
-- UPDATE after OP's comment -- -OP评论后更新-
What about having an object of class Model as a field in your controller? 在控制器中将Model类的对象作为字段怎么办? Something like:
就像是:
class Model {
private StringProperty yyy; // with getters and setters
}
class Controller {
private Model model;
[...]
private void onButtonClicked() {
if (f1.isSelected()) {
model.setYyy(f1.getText())
}
}
}
If you have to share the same model object between different controllers, through the FXMLLoader you can access the controller of a view (with method FXMLLoader.getController()
(after you have called FXMLLoader.load()
) and call the setModel(Model)
method to set the new controller's model 如果必须在不同的控制器之间共享同一模型对象,则可以通过FXMLLoader访问视图的控制器(使用
FXMLLoader.getController()
方法(在调用FXMLLoader.load()
)并调用setModel(Model)
设置新控制器模型的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.