FXML:绑定到控制器

时间:2016-12-24 16:19:03

标签: javafx binding fxml

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

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

的Controler:

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用于从数据库导入数据,它们工作得很好(我放置了断点并检查了它)。问题是,当我更改Label的选择时,我不知道为什么客户ComboBox没有更新,而selectedClient确实发生了变化。我究竟做错了什么?

1 个答案:

答案 0 :(得分:5)

这有多种原因:

  1. 无法观察到简单的字段。
  2. 您未在表达式绑定中包含controller
  3. 这里有一个拼写错误$selecteClient.name$electedClient.phoneNumber
  4. 整个表达式需要包含在{}内以便绑定,而不仅仅是设置。
  5. 你可以,例如像这样修理:

    <强>控制器

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

    (或类似的东西)