DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Binding to a Function in Compiled JavaFX Script

Binding to a Function in Compiled JavaFX Script

James Weaver user avatar by
James Weaver
·
Feb. 15, 08 · Java Zone · Interview
Like (0)
Save
Tweet
7.27K Views

Join the DZone community and get the full member experience.

Join For Free

in 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.

bindtofunctionexample

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

JavaFX Script JavaFX Binding (linguistics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Python: A Befitting Approach to Develop AI Web Apps
  • A Complete Guide About Scaled Agile Framework (SAFe)?
  • Creating Event-Based Architecture on Top of Existing API Ecosystem
  • Growth in Java Development for Web and Mobile Apps

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo