Aubin
Aubin

Reputation: 14853

JavaFx 8 Bidirectional binding

The following code fires java.lang.RuntimeException: A bound value cannot be set:

public class Test {

   public static void main( String[] args ) {
      final DoubleProperty amount = new SimpleDoubleProperty( 100_000.00 );
      final DoubleProperty rate   = new SimpleDoubleProperty();
      final DoubleProperty part   = new SimpleDoubleProperty();
      rate.bind( part.divide  ( amount.getValue()));
      part.bind( rate.multiply( amount.getValue()));
      rate.set( 0.025 );//<<----------------------------------- Here is the cause
      System.out.println( "Part: " + part.get());
      part.set( 1200 );
      System.out.println( "Rate: " + rate.get());
   }
}

Using Bindings.bindBidirectional may be a solution but I don't know how.

Upvotes: 2

Views: 786

Answers (2)

Aubin
Aubin

Reputation: 14853

A simple static method, a homemade NumberBiBinding do the job:

public final class NumberBiBinding {

   public static void bind(
      Property<Number> operand1,
      NumberBinding    operator1,
      Property<Number> operand2,
      NumberBinding    operator2  )
   {
      assert operand1 != operand2;
      operand1.addListener( o -> operand2.setValue( operator1.getValue()));
      operand2.addListener( o -> operand1.setValue( operator2.getValue()));
   }
}

public final class Test {

   public static void main( String[] args ) {
      final DoubleProperty amount = new SimpleDoubleProperty( 100_000.00 );
      final DoubleProperty rate   = new SimpleDoubleProperty();
      final DoubleProperty part   = new SimpleDoubleProperty();

      NumberBiBinding.bind(
         rate, rate.multiply( amount ),
         part, part.divide  ( amount ));

      System.out.println( "Amount: " + amount.get());
      rate.set( 0.025 );
      System.out.println(
        "Part is " + part.get() + " when rate is set to " + rate.get());
      part.set( 1200 );
      System.out.println(
         "Rate is " + rate.get() + " when part is set to " + part.get());
   }
}

Output:

Amount: 100000.0
Part is 2500.0 when rate is set to 0.025
Rate is 0.012 when part is set to 1200.0

Upvotes: 0

chris
chris

Reputation: 1785

A possible solution could be creating InvalidationListeners - not using binding.

How it works:

  • setting rate causes rate to get invalid. This is the time to set part, which therefore is also invalid.
  • Setting rate in turn do NOT fire the InvalidationListener again. This is just the way it is defined.

.

public static void main(String[] args) {
    final DoubleProperty amount = new SimpleDoubleProperty(100_000.00);
    final DoubleProperty part = new SimpleDoubleProperty();
    final DoubleProperty rate = new SimpleDoubleProperty();

    part.addListener(new InvalidationListener() {
        @Override
        public void invalidated(Observable observable) {
            System.out.println("part is invalid");
            rate.set(part.get() / amount.get());
        }
    });

    rate.addListener(new InvalidationListener() {
        @Override
        public void invalidated(Observable observable) {
            System.out.println("rate is invalid");
            part.set(rate.get() * amount.get());
        }
    });

    System.out.println("setting rate");
    rate.set(0.025);
    System.out.println("Part: " + part.get()); //2500

    System.out.println("setting part");
    part.set(1200);
    System.out.println("Rate: " + rate.get()); //0.012
}

The output of the code above is:

setting rate
rate is invalid
part is invalid
Part: 2500.0
setting part
part is invalid
rate is invalid
Rate: 0.012

Upvotes: 2

Related Questions