javafx - FXML:绑定(bind)到 Controller

标签 javafx binding fxml

是否可以像在 xaml 中一样在 FXML 中将 Controller 绑定(bind)到类变量。我正在做的是:

FXML

<ComboBox fx:id="searchField" 
                    HBox.hgrow="ALWAYS" editable="true" />
<GridPane hgap="5" vgap="5">
    <Label text="Nom" />
    <Label text="$selecteClient.name"
        GridPane.columnIndex="1" />

    <Label GridPane.rowIndex="1" text="tél" />
    <Label text="$electedClient.phoneNumber"
        GridPane.rowIndex="1" GridPane.columnIndex="1" />
<GridPane/>

Controller :

private final List<Client> clients = FXCollections.observableArrayList(ImportingDataService.importClients());
@FXML
private Client selectedClient;

@FXML
private ComboBox<Client> searchField;

@Override
public void initialize(URL location, ResourceBundle resources) {
    // Set appDtat client id so it refreshes when client is changed
    this.appState.clientViewClientIDProperty().addListener((obs, oldValue, newValue) -> {
        selectedClient = ImportingDataService.importClient(newValue.longValue());

    });

    // Set up combo box
    setUpComboBox();

}
private void setUpComboBox() {
    searchField.getItems().addAll(clients);
    UtilService.autoCompleteComboBoxPlus(searchField, (typedText, client) -> client.getName().contains(typedText));

    // Handle selecting clients
    searchField.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
        selectedClient = ImportingDataService.importClient(newValue.getId());
    });
}

我的Client 类是一个具有String 字段namephoneNumber 的类。 ImportingDataService.importClient 用于从数据库导入数据,它们工作正常(我放置了断点并进行了检查)。问题是我不知道为什么当我更改 ComboBox 的选择时客户端 Label 不更新,而 selectedClient确实改变。我究竟做错了什么?

最佳答案

这有多种原因:

  1. 一个简单的场是不可观察的。
  2. 您没有在表达式绑定(bind)中包含 controller
  3. 此处 $selecteClient.name$electedClient.phoneNumber 有错别字。
  4. 需要将整个表达式包裹在 {} 中以进行绑定(bind),而不仅仅是设置。

例如,您可以像这样修复它:

Controller

private final ObjectProperty<Client> selectedClient = new SimpleObjectProperty<>(initialClient);

public final Client getSelectedClient() {
    return this.selectedClient.get();
}

public final void setSelectedClient(Client value) {
    this.selectedClient.set(value);
}

public final ObjectProperty<Client> selectedClientProperty() {
    return this.selectedClient;
}

...

// selectedClient = ImportingDataService.importClient(newValue.getId());
setSelectedClient(ImportingDataService.importClient(newValue.getId()));

fxml

<ComboBox fx:id="searchField" 
                    HBox.hgrow="ALWAYS" editable="true" />
<GridPane hgap="5" vgap="5">
    <Label text="Nom" />
    <Label text="${controller.selectedClient.name}"
        GridPane.columnIndex="1" />

    <Label GridPane.rowIndex="1" text="tél" />
    <Label text="${controller.selectedClient.phoneNumber}"
        GridPane.rowIndex="1" GridPane.columnIndex="1" />
<GridPane/>

客户端

public String getName() {
    return name;
}

public String getPhoneNumber() {
    return phoneNumber;
}

(或类似的东西)

关于javafx - FXML:绑定(bind)到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314907/

相关文章:

javascript - JavaFX WebView 禁用同源策略(允许跨域请求)

javafx - JavaFX 选项卡 Pane 下拉列表中不考虑 ImageView 大小

xaml - 根据 bool 值更改 Xamarin 表单标签的文本

wpf - 如何在动态创建的 ContextMenu 中添加水平分隔符?

javafx 自动调整大小和按钮填充

java - 寻找将 FXML 与 Java 7 结合使用的指南

java - 使用场景生成器在 JavaFX 中创建按钮数组

c# - WPF:如何将通用数据绑定(bind)到 TreeView?

css - JavaFX CSS : add inner shadow to a pane itself