Leveraging Java for Great User Experiences and the Internet of Things

December 22, 2008

More Cowbell! An Imaginary Audio Control Example in JavaFX

One of my favorite Saturday Night Live sketches is More Cowbell, in which Christopher Walken's character keeps asking for "more cowbell" during a recording session. Today's example covers some of the simple but powerful concepts of JavaFX in the context of an imaginary iPhone-esque application that lets you select a music genre and control the volume. Of course, "Cowbell Metal", shortened to "Cowbell", is one of the available genres :-) Click the screenshot below to launch the application, and then I'll show you the code behind it.

Application Behavior and the Code Behind It

When you play with the application, notice that adjusting the volume slider changes the associated decibel (dB) level displayed. Also, selecting the Muting checkbox disables the slider, and selecting various genres changes the volume slider. This behavior is enabled by concepts that you'll see in the code below, such as binding to a class that contains a model, on replace triggers, and sequences (think arrays).

Here is the main program, which contains the declarative script that expresses the UI:

/* * AudioConfigMain.fx - A JavaFX Script example program that demonstrates * "the way of JavaFX" (binding to model classes, triggers, sequences, and * declaratively expressed, node-centric UIs). Note: Because this example * covers beginning JavaFX concepts, it is more verbose than necessary. * * Developed 2008 by James L. Weaver jim.weaver [at] javafxpert.com * as a JavaFX Script SDK 1.0 example for the Pro JavaFX book. */package projavafx.audioconfig.ui;

Notice how the bind operator is used in various places to cause the UI to reflect the state of the model. In a couple of places, a bind with inverse is employed to keep the UI and the model class in sync bi-directionally. Now take a look at the model class, and in particular the on replace trigger that is invoked when the user selects a genre:

Comments

"I don't see anything about allowing Java classes to use compiled JavaFX classes just as easily as any other Java class, though, and this is -- as I mentioned -- when we would see a quantum leap in benefit for Java and JavaFX.

Do you know of any news on that front??"

Ari,

I don't know of any news on that front, but the way that I call JavaFX from Java code is by creating Java interfaces (method #1 in the following blog post).

I checked this out, and am glad to hear it -- I like some of the planned changes. I don't see anything about allowing Java classes to use compiled JavaFX classes just as easily as any other Java class, though, and this is -- as I mentioned -- when we would see a quantum leap in benefit for Java and JavaFX.

I know -- but I may not need this enum in my Java code. I'd rather stay with the rapidity of the scripting language where possible, and save Java for the heavy lifting.

This is mainly because I feel that Java and JavaFX are not really playing together well enough yet to intermingle them freely. When I can easily call JavaFX classes from Java, gaining from their binding capabilities and easy declaration (or if you want to give me "bind" and "property" keywords in java), THEN the ease of development of JavaFX will make it into Java, and I will mix-and-match to my heart's content.