你是否可以像在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
类是一个有name
和phoneNumber
这两个String
类型字段的类。而ImportingDataService.importClient
用于从数据库导入数据,它们工作得很好(我设置了断点并检查过)。问题在于,当我改变ComboBox
的选项时,客户端Label
没有更新,而selectedClient
确实发生了变化。我做错了什么?