2016-12-24 13 views
0

FXMLでxamlと同じようにバインドして、コントローラにクラス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はデータベースからデータをインポートするためのもので、うまく動作します(ブレークポイントを設定してチェックしました)。問題は、selectedClientが変更されている間にComboBoxの選択を変更したときにクライアントLabelが更新されない理由がわかりません。私は間違って何をしていますか?

答えて

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

私が言ったことを取り戻します。動いていない。実際には、他の属性にバインドされたラベルをいくつか追加しますが、これはコンボボックス選択モデルを使用することになります。このようにして、オブザーバブルオブジェクトなしで動作します。それ以外の賢さはありません。 –

関連する問題