Jim has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

Binding to a Sequence in Compiled JavaFX Script

02.10.2008
| 5590 views |
  • submit to reddit

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.

Bindingtoasequence_2

/*
* 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

 

Published at DZone with permission of its author, Jim Weaver.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: