The 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() {
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.
JavaFX Script Boot Camp Announcement
As a heads-up, I will be offering a JavaFX Script 2.5 day "boot camp" on Wednesday, April 9 through Friday, April 11, 2008 (ending at noon) in Indianapolis, Indiana. This course is designed to get you quickly up to speed in JavaFX Script application development. A primary reference for this course is my JavaFX Script book, but the course has its own syllabus which includes material covered in the book as well as up to the minute developments in compiled JavaFX Script. Registration will open soon, and for this pilot class I am accepting 12 students. The cost of this pilot class will be 900 USD per student. Additional students from the same organization will be 600 USD. You'll need to bring your laptop computer with the latest versions of the JavaFX Script downloads (which I'll specify in more detail as the class date approaches). The prerequisite for the class will be the completion of a JavaFX Script programming assignment that I'll post soon to this weblog. I'm looking forward to teaching this class and hope that you can attend!
0 comments:
Post a Comment