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 Sequence in Compiled JavaFX Script

Binding to a Sequence in Compiled JavaFX Script

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

Join the DZone community and get the full member experience.

Join For Free

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

Binding (linguistics) JavaFX Script JavaFX

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Programming Habits a Web Developer Should Embrace
  • How to Submit a Post to DZone
  • Which Backend Frameworks Are Impacting Web App Development Immensely?
  • Hard Things in Computer Science

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