1

So I have created Instrument class and Controller class. I have big problem with bindingBidirectional() method. It gives me an error when i'm trying to bind Combobox property with AmountProperty in Instrument class.

amount.valueProperty().bindBidirectional(instrument.amountProperty());

What am I doing wrong here?

Controller class

public class Controller implements Initializable{

@FXML
private ComboBox<Integer> amount = new ComboBox<>();
ObservableList<Integer> amountOptions = FXCollections.observableArrayList(0, 5, 10, 25, 50);

Instrument instrument = new Instrument();

@Override
public void initialize(URL location, ResourceBundle resources) {

    amount.getItems().addAll(amountOptions);
    //THIS ONE IS NOT WORKING
    amount.valueProperty().bindBidirectional(instrument.amountProperty());

}}

And Instrument class:

public class Instrument {

private IntegerProperty amount = new SimpleIntegerProperty();

public int getAmount() {
    return amount.get();
}

public IntegerProperty amountProperty() {
    return amount;
}

public void setAmount(int amount) {
    this.amount.set(amount);
}
}
Jaymin Panchal
  • 2,797
  • 2
  • 27
  • 31
Rocky3582
  • 573
  • 4
  • 7
  • 17
  • What error are you getting? – assylias Dec 26 '16 at 13:44
  • See http://stackoverflow.com/questions/24889638/javafx-properties-in-tableview – James_D Dec 26 '16 at 13:52
  • java: no suitable method found for bindBidirectional(javafx.beans.property.IntegerProperty) method javafx.beans.property.Property.bindBidirectional(javafx.beans.property.Property) is not applicable (argument mismatch; javafx.beans.property.IntegerProperty cannot be converted to javafx.beans.property.Property) method – Rocky3582 Dec 26 '16 at 13:53
  • in bindBidirectional i need to have this: (javafx.bean‌​s.property.Property<‌​java.lang.Integer>), but it says i have this: (jjavafx.beans.property.IntegerProperty), i have no idea how to fix it – Rocky3582 Dec 26 '16 at 13:57

1 Answers1

2

IntegerProperty is an implementation of Property<Number>, not of Property<Integer>. The valueProperty in your combo box is a Property<Integer>. Consequently you cannot bind bidirectionally between the two directly, as the types don't match.

You can either change your combo box to be a ComboBox<Number>, or use IntegerProperty.asObject(), which creates an ObjectProperty<Integer> that is bidirectionally bound to the IntegerProperty:

amount.valueProperty().bindBidirectional(
    instrument.amountProperty().asObject());
James_D
  • 201,275
  • 16
  • 291
  • 322