【问题标题】:How do I bind and unbinddirectional for a StringProperty in a ComboBox javaFX?如何在 ComboBox javaFX 中绑定和取消绑定 StringProperty?
【发布时间】:2020-09-19 16:59:07
【问题描述】:

这就是我从包含一个名为“txtName”的字符串变量的类 FamilyMember 中取消绑定 BiDirectional 的方式。我解除绑定旧值并清除它,然后绑定新值。

解除绑定:

((TreeItem<FamilyMember>)oldValue).getValue().nameProperty().unbindBidirectional(txtName.textProperty());
txtName.clear();

绑定:

txtName.setText(((TreeItem<FamilyMember>)newValue).getValue().nameProperty().getValue());
((TreeItem<FamilyMember>)newValue).getValue().nameProperty().bindBidirectional(txtName.textProperty());

但我对如何为 ComboBox 执行此操作感到困惑。我的组合框用于选择具有 3 个选项的性别作为字符串,(组合框)、男性、女性和其他。如何使用带有字符串属性的 ComboBox 来实现上述目标?

【问题讨论】:

  • minimal reproducible example please .. 那说:comboBox 有一个 value 属性(怀疑你对你真正想要达到的目标的描述是不完整的;)

标签: javafx combobox binding


【解决方案1】:

控制器类:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;

import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    private ComboBox<String> genderComboBox;
    @FXML
    private Label selectedGenderLabel;

    private Random random = new Random();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // Add items:
        genderComboBox.getItems().addAll("Male", "Female", "Other");

        // Bind selection of combo box to string property:
        selectedGenderLabel.textProperty().bind(genderComboBox.valueProperty());
    }

    @FXML
    public void handleUnbindBtnClick() {
        // Un-bind and clear:
        selectedGenderLabel.textProperty().unbind();
        selectedGenderLabel.setText("");
    }

    @FXML
    public void handleBindBtnClick() {
        // Make a random selection:
        genderComboBox.getSelectionModel().select(random.nextInt(3));

        // Re-bind:
        selectedGenderLabel.textProperty().bind(genderComboBox.valueProperty());
    }
}

FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>

<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <ComboBox fx:id="genderComboBox" />
      <Label text="Selected Gender:" />
      <Label fx:id="selectedGenderLabel" />
      <Button mnemonicParsing="false" onAction="#handleUnbindBtnClick" text="Unbind" />
      <Button mnemonicParsing="false" onAction="#handleBindBtnClick" text="Bind" />
   </children>
</VBox>

【讨论】:

  • 您可以(并且应该 - 规则是:尽可能避免路径绑定)绑定到组合的值 - 保证与 selectedItem 相同。而且我不知道为什么是中间 stringProperty?
  • 好的,谢谢。我删除了中间 StringProperty 并更改为 valueProperty()。你能告诉我当我使用 getSelectionModel().getSelectedItem() 时会出现什么问题吗?或者在哪里阅读该规则?
  • 路径属性的一般问题是您必须防止所有父属性的更改,并在绑定/侦听器发生更改时重新连接。这里将监听 selectionModelProperty 并更新与 selectedItem 的绑定 - 不会经常发生,但没有代码是脆弱的。
猜你喜欢
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
相关资源
最近更新 更多