【问题标题】:FXML: Binding to controllerFXML:绑定到控制器
【发布时间】:2016-12-24 16:19:02
【问题描述】:

是否可以像在 xaml 中一样在 FXML 中将控制器绑定到类变量。我正在做的是:

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/>

控制器:

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 的选择时客户端Labels 没有更新,而selectedClient 确实发生了变化。我究竟做错了什么?

【问题讨论】:

    标签: javafx binding fxml


    【解决方案1】:

    这有多种原因:

    1. 简单的字段是不可观察的。
    2. 您没有在表达式绑定中包含controller
    3. $selecteClient.name$electedClient.phoneNumber 有错别字。
    4. 整个表达式需要包裹在{} 中才能被绑定,而不仅仅是设置。

    你可以例如像这样修复它:

    控制器

    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;
    }
    

    (或类似的东西)

    【讨论】:

    • 这是一个很好的完整答案,但它只适用于修复错别字。可观察对象不是必需的。谢谢。
    • 我收回我说的话。它不工作。实际上,我添加了一些将它们绑定到其他属性的标签,但这是使用组合框选择模型,并且以这种方式,它可以在没有可观察对象的情况下工作。否则它不会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2013-10-26
    • 2013-12-25
    • 1970-01-01
    • 2014-02-25
    • 2023-03-19
    相关资源
    最近更新 更多