« Getting "Plutoed" - Sequence Manipulation in Compiled JavaFX Script | Main | Trigger Happy - Sequence Triggers in Compiled JavaFX Script »

February 10, 2008

Binding to a Sequence in Compiled JavaFX Script

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() {
                  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

Javafxpertbootcamp300_x_250_banne_2

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!

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





TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54f133d69883400e5503a79f78834

Listed below are links to weblogs that reference Binding to a Sequence in Compiled JavaFX Script:

Comments

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

My Photo

Upcoming Speaking Engagements:


  • Stephen Chin and Jim Weaver speaking about JavaFX Platform

  • Speaking on JavaFX and Java at Øredev in Malmö, Sweden on 2-6 November, 2009

Upcoming JavaFX Training:


  • Developing Secure, Rich Internet Applications Hosted on a Variety of Clients Using JavaFX Technology

Enter your email address:

Delivered by FeedBurner

Available now as early access eBook


  • Click book image above to obtain eBook

Twitter Updates

    follow me on Twitter

    Affiliations:

    DZone Links:


    July 2009

    Sun Mon Tue Wed Thu Fri Sat
          1 2 3 4
    5 6 7 8 9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30 31  

    Disclaimer:

    • By reading this site, you are agreeing that under no circumstances will Veriana Networks, Inc. or its affiliates be responsible for (1) any information contained on or omitted from the site, (2) any person's reliance on any such information, whether or not the information is correct, current or complete, (3) the consequences of any action you or any other person takes or fails to take, whether or not based on information provided by or as a result of the use of the sites.