Created
June 10, 2015 20:23
-
-
Save jspdown/bfe494e8acb5fc507dba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.beans.InvalidationListener; | |
import javafx.beans.binding.*; | |
import javafx.beans.property.FloatProperty; | |
import javafx.beans.property.IntegerProperty; | |
import javafx.beans.property.SimpleFloatProperty; | |
import javafx.beans.property.SimpleIntegerProperty; | |
import javafx.beans.value.ChangeListener; | |
public class Example02 { | |
private static IntegerProperty ip = new SimpleIntegerProperty(-100); | |
public static void main(String[] args) { | |
createProperty(); | |
testChangeListeners(); | |
testBinding(); | |
testBiDirectionalBinding(); | |
testCalculatedValueBinding(); | |
} | |
private static void createProperty() { | |
ip = new SimpleIntegerProperty(301); | |
} | |
public static void testChangeListeners() { | |
ChangeListener listener = (observable, oldValue, newValue) -> | |
System.out.printf("Value changed, old_value = %s, new_value = %s\n", oldValue, newValue); | |
ip.addListener(listener); | |
ip.set(100); | |
ip.removeListener(listener); | |
} | |
public static void testBinding() { | |
IntegerProperty anotherIp = new SimpleIntegerProperty(0); | |
anotherIp.bind(ip); | |
ip.set(500); | |
System.out.printf("ip.get() = %s\n", ip.get()); | |
anotherIp.unbind(); | |
} | |
private static void testBiDirectionalBinding() { | |
IntegerProperty anotherIp = new SimpleIntegerProperty(0); | |
anotherIp.bindBidirectional(ip); | |
ip.setValue(100); | |
anotherIp.setValue(200); | |
ip.unbindBidirectional(anotherIp); | |
} | |
public static void testCalculatedValueBinding() { | |
FloatProperty width = new SimpleFloatProperty(10.0f); | |
FloatProperty height = new SimpleFloatProperty(20.0f); | |
FloatProperty radius = new SimpleFloatProperty(1.5f); | |
FloatBinding rectArea = new FloatBinding() { | |
// unnamed constructor | |
{ | |
// binding this binding to the width and height properties | |
// i.e. if either one changes, computeValue will be called | |
super.bind(width, height); | |
} | |
@Override | |
protected float computeValue() { | |
return width.get() * height.get(); | |
} | |
}; | |
rectArea.addListener((observable, oldValue, newValue) -> { | |
System.out.println(">> Rectangle area changed to " + newValue.intValue()); | |
}); | |
// create a binding using the Fluent API, areaCircle = PI * radius * radius | |
NumberBinding areaCircle = radius.multiply(radius).multiply(Math.PI); | |
areaCircle.addListener((observable, oldValue, newValue) -> { | |
System.out.println(">> Circle area changed to " + newValue.intValue()); | |
}); | |
// create a binding that adds multiple things together | |
NumberBinding total = width.add(height).add(radius); | |
total.addListener((observable1, oldValue1, newValue) -> { | |
System.out.println(">> Total changed to " + newValue.intValue()); | |
}); | |
// can create bindings with other types too | |
StringBinding message = (StringBinding) Bindings | |
.concat("Circle area = ") | |
.concat(areaCircle) | |
.concat(", Rectangle area = ") | |
.concat(rectArea); | |
message.addListener((observable, oldValue, newValue) -> { | |
System.out.printf(">> Message changed to '%s'\n", newValue); | |
}); | |
// can use simple logic using the Fluent API as well | |
StringBinding largerMessage = (StringBinding) Bindings | |
.when(rectArea.greaterThan(areaCircle)) | |
.then(Bindings.concat("Larger area is rectangle = ").concat(rectArea)) | |
.otherwise(Bindings.concat("Larger area is circle = ").concat(areaCircle)); | |
largerMessage.addListener((observable, oldValue, newValue) -> { | |
System.out.printf(">> Larger message changed to '%s'\n", newValue); | |
}); | |
// display the area | |
System.out.println("Displaying current values..."); | |
System.out.printf("width.get() = %s\n", width.get()); | |
System.out.printf("height.get() = %s\n", height.get()); | |
System.out.printf("rectArea.get() = %s\n", rectArea.get()); | |
System.out.printf("circleArea.getValue() = %s\n", areaCircle.getValue()); | |
System.out.printf("total.getValue() = %s\n", total.getValue()); | |
System.out.printf("message.getValue() = '%s'\n", message.getValue()); | |
System.out.printf("largerMessage.getValue() = '%s'\n", largerMessage.getValue()); | |
System.out.println(); | |
// change property values | |
System.out.println("Changing property values..."); | |
width.set(25.0f); | |
height.set(17.25f); | |
radius.set(12.45f); | |
System.out.println(); | |
System.out.println("Displaying current values..."); | |
System.out.printf("width.get() = %s\n", width.get()); | |
System.out.printf("height.get() = %s\n", height.get()); | |
System.out.printf("rectArea.get() = %s\n", rectArea.get()); | |
System.out.printf("circleArea.getValue() = %s\n", areaCircle.getValue()); | |
System.out.printf("total.getValue() = %s\n", total.getValue()); | |
System.out.printf("message.getValue() = '%s'\n", message.getValue()); | |
System.out.printf("largerMessage.getValue() = '%s'\n", largerMessage.getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment