Help me fix my Java program project error !! Thank you!! Add BindingDemo.java to
ID: 3796100 • Letter: H
Question
Help me fix my Java program project error !! Thank you!!
Add BindingDemo.java to your project. The program has an error. Try to fix the problem. Hint: you cannot change the value of a target after the binding, since it is already bound to the source.
If we change the unidirectional binding to bidirectional binding, we will be able to change the value of both d1 and d2. Reload the original BindingDemo.java, change the bind method to bindBidirectional. Run the program again. There should be no error.
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
public class BindingDemo {
public static void main(String[] args) {
DoubleProperty d1 = new SimpleDoubleProperty(1);
DoubleProperty d2 = new SimpleDoubleProperty(2);
d1.bind(d2);
System.out.println("d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
d1.setValue(50.2);
System.out.println("d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
d2.setValue(70.2);
System.out.println("d1 is " + d1.getValue()
+ " and d2 is " + d2.getValue());
}
}
Explanation / Answer
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
public class BindingDemo {
public static void main(String[] args) {
DoubleProperty d1 = new SimpleDoubleProperty(1);
DoubleProperty d2 = new SimpleDoubleProperty(2);
DoubleProperty d3 = new SimpleDoubleProperty(3);
d1.bind(d2.multiply(d2));
d3.bindBidirectional(d2);
System.out.println("d1 is " + d1.getValue() + ", d2 is "
+ d2.getValue() + " and d3 is " + d3.getValue());
d2.setValue(88.8);
System.out.println("d1 is " + d1.getValue() + ", d2 is "
+ d2.getValue() + " and d3 is " + d3.getValue());
d3.setValue(14.4);
System.out.println("d1 is " + d1.getValue() + ", d2 is "
+ d2.getValue() + " and d3 is " + d3.getValue());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.