3

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.

Joe
  • 45
  • 1
  • 5
  • 3
    *"I can see why this is the case"* I don't: Check the Javadoc. It's the second interface mentioned in *All Implemented Interfaces*: https://docs.oracle.com/javase/9/docs/api/javafx/beans/property/DoubleProperty.html Did you import the correct `Observable` class, i.e. `javafx.beans.Observable`? – fabian Apr 30 '18 at 11:17
  • 1
    Probably you have the wrong import for `Observable` – James_D Apr 30 '18 at 11:17
  • You are both correct. I accidentally imported `java.util.Observable` instead of `javafx.beans.Observable`. Don't I feel like an idiot... – Joe Apr 30 '18 at 11:24

1 Answers1

3

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.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • Correct. I accidentally imported `java.util.Observable` instead of `javafx.beans.Observable`. Shame on me! And thank you for going to the trouble of compiling code. – Joe Apr 30 '18 at 11:24
  • @Joe No problem, it happens to us all...! – Michael Berry Apr 30 '18 at 11:26