猿问

Javafx单选按钮定向绑定

我还是 javafx 的新手,我想对单选按钮执行定向绑定


在我的 fxml 我有


  <fx:define>

          <ToggleGroup fx:id="version_selection" />                  

  </fx:define> 

  <RadioButton toggleGroup="$version_selection" ............> 

  <RadioButton toggleGroup="$version_selection" ............> 

在我的控制器中,我想绑定所选切换组的值。使用文本字段很简单


 @FXML

TextField name;


@FXML

private ToggleGroup version_selection;


  name.textProperty().bindBidirectional(model.field5Property());

其中模型是我的具有 SimpleStringValue 属性的类


我如何绑定切换组,因为它没有文本属性作为文本字段


达令说
浏览 196回答 1
1回答

HUH函数

即使ObjectProperty<Toggle>您的模型中有 a ,您也无法建立双向绑定,因为ToggleGroup.selectedToggle它是只读的。ToggleGroup.selectedToggle您需要通过向属性和模型属性添加侦听器来解决此问题,并在更改时更新更改源以外的属性。使用 aMap<String, Toggle>将文本转换为要选择的切换。以下示例使用 aTextField来确定属性而不是模型属性来演示行为:输入RadioButton要选择的文本,它将被选中。@Overridepublic void start(Stage primaryStage) throws IOException {&nbsp; &nbsp; ToggleGroup group = new ToggleGroup();&nbsp; &nbsp; VBox vbox = new VBox();&nbsp; &nbsp; for (char c = 'a'; c <= 'f'; c++) {&nbsp; &nbsp; &nbsp; &nbsp; RadioButton radio = new RadioButton(Character.toString(c));&nbsp; &nbsp; &nbsp; &nbsp; radio.setToggleGroup(group);&nbsp; &nbsp; &nbsp; &nbsp; vbox.getChildren().add(radio);&nbsp; &nbsp; }&nbsp; &nbsp; TextField input = new TextField();&nbsp; &nbsp; vbox.getChildren().add(input);&nbsp; &nbsp; // create map mapping from text to corresponding toggle&nbsp; &nbsp; Map<String, Toggle> map = new HashMap<>();&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (Toggle t : group.getToggles()) {&nbsp; &nbsp; &nbsp; &nbsp; map.put(((RadioButton) t).getText(), t);&nbsp; &nbsp; }&nbsp; &nbsp; StringProperty property = input.textProperty();&nbsp; &nbsp; InvalidationListener listener = new InvalidationListener() {&nbsp; &nbsp; &nbsp; &nbsp; // flag preventing circular updating&nbsp; &nbsp; &nbsp; &nbsp; boolean updating = false;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void invalidated(Observable observable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!updating) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updating = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (observable == group.selectedToggleProperty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // change as result of user interaction with RadioButton&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // update property&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RadioButton selectedToggle = (RadioButton) group.getSelectedToggle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; property.set(selectedToggle == null ? "" : selectedToggle.getText());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // change as result of updating the property&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // select corresponding toggle&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toggle toggleToSelect = map.get(property.get());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group.selectToggle(toggleToSelect);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updating = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; property.addListener(listener);&nbsp; &nbsp; group.selectedToggleProperty().addListener(listener);&nbsp; &nbsp; Scene scene = new Scene(vbox);&nbsp; &nbsp; primaryStage.setScene(scene);&nbsp; &nbsp; primaryStage.show();}
随时随地看视频慕课网APP

相关分类

Java
我要回答