JavaFX SimpleDoubleProperty create

Description

JavaFX SimpleDoubleProperty create

import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

public class Main {
   public static void main(String[] args) {
      // volume = 4/3 * pi r^3
      final DoubleProperty radius = new SimpleDoubleProperty(2);

      DoubleBinding volumeOfSphere = new DoubleBinding() {
          {/*w ww . j av a2s  .com*/
              super.bind(radius);
          }

          @Override
          protected double computeValue() {
              return (4 / 3 * Math.PI * Math.pow(radius.get(), 3));
          }
      };

      System.out.println("Current - radius for Sphere: " + radius.get());
      System.out.println("Current - volume for Sphere: " + volumeOfSphere.get());
      System.out.println("Modifying DoubleProperty radius");
      
      radius.set(50);
      System.out.println("After - radius for Sphere: " + radius.get());
      System.out.println("After - volume for Sphere: " + volumeOfSphere.get());
   }
}



PreviousNext

Related