Binding to a Sequence in Compiled JavaFX Script
Join the DZone community and get the full member experience.
Join For Freethe javafx script compiler team has made great strides recently in the area of sequence binding. for a refresher on sequences, see the getting plutoed post. here's an example of binding a listbox to a sequence so that when the sequence is modified the listbox stays in sync with the elements in the sequence. compile and run this example, clicking on words in the listbox, and a entering a word followed by activating the add button. i'll finish up with a brief discussion on the binding aspects of this example.
/*
* listboxsequencebinding.fx
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example of binding a listbox to
* a sequence.
*/
import javafx.ui.*;
import java.lang.system;
class model {
attribute entries:string[] = ["red", "green", "blue", "yellow", "purple"];
attribute selectedindex:number = 0 on replace {
system.out.println("selectedindex={selectedindex}");
};
attribute entrytoadd:string;
}
frame {
var mdl = model {}
title: "binding to a sequence"
width: 300
height: 200
background: color.white
visible: true
content:
borderpanel {
center:
flowpanel {
content:
listbox {
cells: bind for (entry in mdl.entries)
listcell {
text: entry
}
selection: bind mdl.selectedindex with inverse
fixedcellwidth: 100
visiblerowcount: 4
}
}
bottom:
flowpanel {
content: [
textfield {
value: bind mdl.entrytoadd with inverse
columns: 10
},
button {
text: "add"
defaultbutton: true
enabled: bind mdl.entrytoadd.trim() <> ""
action:
function():void {
insert mdl.entrytoadd into mdl.entries;
mdl.entrytoadd = "";
system.out.println("sizeof entries={sizeof mdl.entries}");
}
}
]
}
}
}
binding aspects of this program
the main purpose of this post is to teach you about binding to a sequence, so i'll point that out first: as shown in the following excerpt from the program, the
cells
attribute of the
listbox
is bound to the
entries
sequence contained in the model:
cells: bind for (entry in mdl.entries)
listcell {
text: entry
}
to accomplish this bind, we're enlisting the help of the
for
expression whose value is a sequence of listcell instances. each of these instances has an element of the
entries
sequences assigned to its
text
attribute. as the
entries
sequence is modified (for example by adding an element) the
for
expression is re-eavaluated, causing the listbox to be dynamically updated. to learn more about the
for
expression, see
this post
.
another bind in this program keeps the textfield in sync with the
entrytoadd
attribute in the model, as shown in this expert:
textfield {
value: bind mdl.entrytoadd with inverse
columns: 10
},
this is a bi-directional bind, as signified by the
with inverse
keywords. there is another ui component that binds with the entrytoadd attribute, which causes the
ok
button to be enabled only when the
textfield
contains text:
button {
text: "add"
defaultbutton: true
enabled: bind mdl.entrytoadd.trim() <> ""
...code omitted...
}
lastly, the following bind causes the
selected
attribute of the listbox to be in sync with the
selectedindex
attribute of the model. to demonstrate this, its value is printed to the console in the replace trigger, which is executed whenever the value changes.
as you can see, binding ui components to a model is a very powerful concept that greatly simplifies application development. as always, please post a comment if you have any questions.
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