Don't understand how to use changlistener in JavaFX


Question

I am trying to add a changlistener to Scene.getWidth() so I can detect a resize. This is a code snipplet of how I tried to add the listener.

scene.getWidth().addListener(new ChangeListener<Double>() {
    public void changed(ObservableValue<? extends Double> ov,double old_val, double new_val) {
        System.out.println(new_val);
    }
});

It gives me the followig errors.

./GraphFX.java:49: error: is not abstract and does not override abstract method changed(ObservableValue,Double,Double) in ChangeListener scene.getWidth().addListener(new ChangeListener() {

^
./GraphFX.java:49: error: double cannot be dereferenced scene.getWidth().addListener(new ChangeListener() { ^ 2 errors

1
3
1/13/2013 3:24:22 PM

scene.getWidth() returns a double primitive not an object thats why it cannot be dereferenced .public final double getWidth()

you can use scene widthProperty() method which returns ReadOnlyDoubleProperty

 scene.widthProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue ov, Object t, Object t1) {
         double value = (double) t1;
            System.out.println("Scene Width :" + value);
        }

   });

alternatively you can track scenewidth changes by just binding scene widthproperty

code :

DoubleProperty sceneWidth = new SimpleDoubleProperty();
sceneWidth.bind(scene.widthProperty());
2
1/13/2013 7:39:58 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon