javajavafxjava-8observablelistextractor

Cannot instantiate ObservableList with an extractor


I have a custom object FermentableInRecipe, which populates a TableView. In order to respond to changes to items in the list, as well as the list itself, I have decided to employ an extractor. Here is my declaration and instantiation of my ObservableList:

private ObservableList<FermentableInRecipe> fermentablesInRecipe = 
FXCollections.observableArrayList(item -> new Observable[]{item.WeightProperty()});

Here are the relevant segments of my custom class:

public class FermentableInRecipe {

    private DoubleProperty weight;

    ...

    public Double getWeight() {
         return this.weight.getValue();
    }

    public void setWeight(Double value) {
        this.weight.setValue(value);
    }

    public DoubleProperty WeightProperty() {
        if (weight == null) {
            weight = new SimpleDoubleProperty(0.0);
        }
        return weight;
    }

    ...
}

In the links I've provided below, this approach worked. But Netbeans is telling me "DoubleProperty cannot be converted to Observable". I can see why this is the case, but I cannot understand why it worked in the links below and not for me, and how I should create extractor and link it to the weightProperty() function if this approach doesn't work.

Links:

JavaFX 2.0 Choice Box Issue. How to update a choiceBox, which represents a list of objects, when an object is updated?

JavaFX, ObservableList: How to fire an InvalidationListener whenever an object of the list gets modified?

Thanks in advance. Let me know if I've missed any crucial information.


Solution

  • There's nothing wrong with your code as written, this compiles just fine for me:

    import javafx.application.Application;
    import javafx.beans.Observable;
    import javafx.beans.property.DoubleProperty;
    import javafx.beans.property.SimpleDoubleProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.stage.Stage;
    
    public class JavaFXApplication1 extends Application {
    
        class FermentableInRecipe {
    
            private DoubleProperty weight;
    
            public Double getWeight() {
                return this.weight.getValue();
            }
    
            public void setWeight(Double value) {
                this.weight.setValue(value);
            }
    
            public DoubleProperty WeightProperty() {
                if (weight == null) {
                    weight = new SimpleDoubleProperty(0.0);
                }
                return weight;
            }
    
        }
    
        private ObservableList<FermentableInRecipe> fermentablesInRecipe = FXCollections.observableArrayList(item -> new Observable[]{item.WeightProperty()});
    
        @Override
        public void start(Stage primaryStage) throws Exception {
        }
    }
    

    I'd suggest double checking imports, and make sure you haven't imported java.util.Observable or similar by mistake.