Binding to a Function in Compiled JavaFX Script
Join the DZone community and get the full member experience.
Join For Freein previous posts i've demonstrated binding to an attribute , and binding to a sequence . now i'd like to show you how to bind to a function. this is a very powerful feature because whenever the value of the function changes, the bound variable is updated. here's a screenshot of today's example program, in which the user selects the diameter of a circle from a slider widget. the program calculates and displays the area of the circle, as well as drawing a circle with the selected diameter in pixels.
here's the javafx script code for this example:
/*
* bindtofunctionexample.fx - a compiled javafx program that demonstrates
* binding to a function.
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example.
*/
import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.math;
import java.lang.system;
class circlemodel {
attribute diameter:number;
function getarea():number {
math.pi * math.pow(diameter / 2, 2);
}
}
frame {
var cmodel = circlemodel {}
width: 480
height: 560
title: "bind to function example"
background: color.white
content:
borderpanel {
center:
canvas {
content: [
circle {
cx: 240
cy: 250
radius: bind cmodel.diameter * 2
stroke: color.purple
strokewidth: 1
fill: color.cyan
},
text {
font:
font {
face: fontface.sansserif
style: fontstyle.bold
size: 24
}
x: 20
y: 10
stroke: color.red
fill: color.red
content: bind "diameter: {cmodel.diameter}"
},
text {
font:
font {
face: fontface.sansserif
style: fontstyle.bold
size: 24
}
x: 240
y: 10
stroke: color.red
fill: color.red
content: bind "area: {%3.2f cmodel.getarea()}"
}
]
}
bottom:
slider {
min: 0
max: 100
border:
titledborder {
title: "diameter:"
}
value: bind cmodel.diameter with inverse
minortickspacing: 5
majortickspacing: 10
paintticks: true
paintlabels: true
labels: [
sliderlabel {
value: 0
label:
simplelabel {
text: "0"
}
},
sliderlabel {
value: 50
label:
simplelabel {
text: "50"
}
},
sliderlabel {
value: 100
label:
simplelabel {
text: "100"
}
}
]
}
}
visible: true
onclose:
function():void {
system.exit(0);
}
}
binding to a function
as shown above, the slider is bi-directionally bound to the diameter attribute of the circlemodel instance. this attribute affects the value of the getarea() function, so as the value of the diameter attribute changes, the content attribute bound to the getarea() function will reflect its value.
formatting output
another thing that you'll notice in this example is that the value of the getarea() function is formatted to two decimal places when displayed in the text graphical node. this is due to the formatting capabilities of javafx script that i began discussing in the happy new year post.
regards,
jim weaver
javafx script: dynamic java scripting for rich internet/client-side applications
immediate ebook (pdf) download available at
the book's apress site
Opinions expressed by DZone contributors are their own.
Comments