2016-12-24 179 views
0

是否可以像在xaml中一樣將FXML綁定到控制器到類varibale。我做的是: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/> 

的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用於從數據庫導入數據,它們工作得很好(我放置了斷點並檢查了它)。問題是我不知道當我更改ComboBox的選擇時,爲什麼客戶端Label s不更新,而selectedClient確實發生了更改。我究竟做錯了什麼?

回答

3

有多種原因:

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

(或SOM類似的)

+0

這是一個不錯的完整答案,但它確實可以解決錯別字的問題。可觀察的對象不是必需的。謝謝。 –

+0

我收回我說的話。它不工作。實際上,我添加了一些標籤將它們綁定到其他屬性,但這是使用組合框選擇模型,並且以這種方式,它沒有可觀察對象。其他方面則不然。 –