How to do bidirectional binding between a StringProperty and a combobox SelectedItemProperty

I want to have a bidirectional binding between a StringProperty and a combobox SelectedItemProperty. Whenever the selected item of the combox changes, it should reflect the value to the StringProperty. Likewise, whenever the value of the StringProperty changes, it should select the value in the combobox.

How can we do this binding?

Upvotes: 0

Views: 480

Answers (1)

Zephyr
Zephyr

Reputation: 10253

You can not bind to the SelectedItemProperty, but would instead add the binding to the ValueProperty of the ComboBox:

comboBox.valueProperty().bindBidirectional(stringProperty);

Here is a complete example to demonstrate:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class BiBindingExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Our StringProperty
        StringProperty stringProperty = new SimpleStringProperty();

        // Label to display our StringProperty
        Label label = new Label();
        label.textProperty().bind(stringProperty);

        // The ComboBox
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("Zero", "One", "Two", "Three", "Four", "Five");

        // Bind the ComboBox value to that of the StringProperty, and vice versa
        comboBox.valueProperty().bindBidirectional(stringProperty);

        // A button to programmatically change the StringProperty
        Button button = new Button("Return to Zero");
        button.setOnAction(e -> stringProperty.set("Zero"));

        root.getChildren().addAll(comboBox, label, button);

        // Show the Stage
        primaryStage.setWidth(300);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

Upvotes: 4

Related Questions