« JavaFX Puzzler 3 - First Correct Solution Gets Free JavaFX Script eBook | Main | Preview of Compiled JavaFX Script Syntax on PlanetJFX »

November 14, 2007

BindListBoxToSequence (JavaFX Puzzler 3) Results and Sample Solution

Congratulations to Lucas Benedicic for submitting the first solution to the BindListBoxToSequence Puzzler, and the fact that it met the requirements of the problem!

For reference, here is my sample solution, followed by the screenshot:

/*
* BindListBoxToSequence.fx
*/

import javafx.ui.*;

class ListBoxModel {
  attribute entries:String*;
}
attribute ListBoxModel.entries = [];

Frame {
  var model =
    ListBoxModel {
    }
  var elementTextField =
    TextField {
      columns: 12
    }
  title: "Bind ListBox to Sequence Puzzler"
  content:
    BorderPanel {
      top:
        FlowPanel {
          content: [
            elementTextField,
            Button {
              text: "Add Element"
              defaultButton: true
              action:
                operation() {
                  insert elementTextField.value into model.entries;
                  elementTextField.value = "";
                }
            }
          ]
        }
      center:
        FlowPanel {
          content:
            ListBox {
              fixedCellWidth: 200
              cells: bind foreach (entry in model.entries)
                ListCell {
                  text: entry
                }
            }
        }
    }
  width: 400
  height: 250
  visible: true
}

Bindlistboxtosequence

Congratulations again, Lucas!  I'll contact you right away with instructions on downloading the JavaFX Script eBook.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download available at the book's Apress site

TrackBack

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

Listed below are links to weblogs that reference BindListBoxToSequence (JavaFX Puzzler 3) Results and Sample Solution:

Comments

Nice work, Craig! By the way, if the application had a menu, detecting the Delete key could be accomplished as shown in the following snippet from the JavaFX Script book:

MenuItem {
text: "Delete Word"
mnemonic: D
accelerator: {
keyStroke: DELETE
}
enabled: bind not wgModel.fillLettersOnGrid
action: operation() {
wsHandlers.wordListDeleteWord();
}
}

Thanks,
Jim Weaver

Jim, I came up with a solution for deleting an item from the ListBox when the user hits the "Delete" key....not sure how this code is going to look in the comment, but here it goes:

import javafx.ui.*;
import java.lang.System;

class ListBoxModel {
attribute entries:String*;
attribute selIndex:Number;
}
attribute ListBoxModel.entries = [];

Frame {
var model =
ListBoxModel {
selIndex: 0
}
var elementTextField =
TextField {
columns: 12
}
var theListBox =
ListBox {
fixedCellWidth: 200
cells: bind foreach(entry in model.entries)
ListCell {
text: entry
}
selection: bind model.selIndex
toolTipText: "Select an item and hit the delete key to remove it"
onKeyDown:
operation(e:KeyEvent) {
if (e.keyStroke == DELETE:KeyStroke and model.selIndex > -1) {
delete model.entries[model.selIndex];
}

}
}

title: "Bind ListBox to Sequence Puzzler"
content:
BorderPanel {
top:
FlowPanel {
content: [
elementTextField,
Button {
text: "Add Element"
defaultButton: true
action:
operation() {
insert elementTextField.value into model.entries;
elementTextField.value = "";
System.out.println("Test2");
}
toolTipText: "Add an element"
}
]
}
center:
FlowPanel {
content:
[
theListBox,
]
}
}
width: 400
height: 250
visible: true
}

I did a couple things to make this work. I added an attribute to the model named, "selIndex" which gets bound to the "selection" attribute of the ListBox. Then I implemented the "onKeyDown" function in the ListBox which handles deleting the element out of the model.

Finding the "DELETE:KeyStroke" was a bit tricky, I did some Googling and came across another post where someone demonstrated using "RIGHT:KeyStroke" and "LEFT:KeyStroke" to determine if the Left or Right keys were pressed, so I figured "DELETE" might be what I needed, and it was :)

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.