2016-12-26 3 views
0

So habe ich Instrument Klasse und Controller Klasse erstellt. Ich habe ein großes Problem mit bindingBidirectional() Methode. Es gibt mir einen Fehler, wenn ich Combobox Eigenschaft mit AmountProperty in Instrument Klasse zu binden bin versucht.[JavaFx] Was ist falsch an dieser Bindung?

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

Was mache ich hier falsch?

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()); 

}} 

Und Instrument Klasse:

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); 
} 
} 
+0

Welche Fehler erhalten Sie? – assylias

+0

Siehe http://stackoverflow.com/questions/24889638/javafx-properties-in-tableview –

+0

java: Keine geeignete Methode gefunden für bindBidirectional (javafx.beans.property.IntegerProperty) Methode javafx.beans.property.Property.bindBidirectional (javafx.beans.property.Property ) ist nicht anwendbar (Argument stimmt nicht überein; javafx.beans.property.IntegerProperty kann nicht in javafx.beans.property.Property konvertiert werden) Methode – Rocky3582

Antwort

1

IntegerProperty ist eine Implementierung von Property<Number>, nicht von Property<Integer>. Die valueProperty in Ihrer Combo-Box ist eine Property<Integer>. Folglich können Sie nicht direkt bidirektional zwischen den beiden binden, da die Typen nicht übereinstimmen.

Sie können entweder Ihr Kombinationsfeld ändern ein ComboBox<Number>, oder verwenden Sie IntegerProperty.asObject() zu sein, das schafft eine ObjectProperty<Integer>, die bidirektional mit dem IntegerProperty gebunden ist:

amount.valueProperty().bindBidirectional(
    instrument.amountProperty().asObject()); 
+0

Vielen Dank, jetzt verstehe ich :) – Rocky3582

+0

@ Rocky3582 Bitte markieren Sie die Antwort als richtig, wenn es Ihre Frage beantwortet –

Verwandte Themen