Home > Articles > Programming > Java

Bindstorming and JavaFX Performance

  • Print
  • + Share This
Jim Connors, co-author of JavaFX: Developing Rich Internet Applications, explains bindstorming, when a multitude of JavaFX bind recalculations severely hampers interactive performance.
From the author of

It is within our nature, even in the most infinitesimal way, to leave our mark on this world before we exit it. I'd like to coin the following term, heretofore unseen in the JavaFX space, and submit it as my humble contribution to the human collective:

bindstorm \'bïnd•storm\ (noun): condition where a multitude of JavaFX bind recalculations severely hampers interactive performance

Yeah, I know, using the word you wish to define inside its definition is bad, but there is precedent for this: (1) Fancy-schmancy, hoity-toity college dictionaries do it all the time. (2) Mathematicians and computer scientists call this recursion: that mysterious concept which developers use to impress others of their programming prowess.

Don't get me wrong, JavaFX binding is incredibly powerful. Heck, we dedicated a whole chapter to it in our new book JavaFX: Developing Rich Internet Applications. But binding does come with a price, and like most anything else, over-consumption can lead to abuse.

Consider this use case: you've got a JavaFX application with dozens or maybe even hundreds of Nodes that are part of the scenegraph. Each of the Nodes are ultimately sized and positioned in proportion to height and width instance variables that are passed on down. If you define width and height at startup and have no interest in a resizeable interface, then you stand a good chance of avoiding the use of many bind expressions. The one potential twist here is that if you're sincerely interested in a non-resizeable application, but want it to consume the entire screen, what do you do? As screens come in all shapes and sizes, you may not know what the resolution is at start time. JavaFX has an elegant solution for this which uses binding.

Here's a simple application which defines a Rectangle and Circle that fill the entire screen. You can click anywhere within the Circle to exit the application. Notice the number of binds required to get this to work.

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;

function run() : Void {
    var stage: Stage = Stage {
        fullScreen: true
        scene: Scene {
            content: [
                Rectangle {
                    width: bind stage.width
                    height: bind stage.height
                    fill: Color.BLUE
                }
                Circle {
                    centerX: bind stage.width / 2
                    centerY: bind stage.height / 2
                    radius: bind if (stage.width < stage.height) then
                            stage.width / 2 else stage.height / 2
                    fill: Color.RED
                    onMouseClicked: function(me: MouseEvent) {
                        FX.exit();
                    }
                }
            ]
        }
    }
}

Imagine what this would look like if you had lots of complex custom components with many more dependencies on height and width. In addition to the potential performance impact, this could be error-prone and cumbersome to code. To avoid the over usage of binding and the potential for a bindstorm, applications of this sort could be re-written as follows:

import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;

function run() : Void {
    var AWTtoolkit = java.awt.Toolkit.getDefaultToolkit ();
    var screenSizeFromAWT = AWTtoolkit.getScreenSize ();
    Stage {
        fullScreen: true
        scene: Scene {
            content: [
                Rectangle {
                    width: screenSizeFromAWT.width
                    height: screenSizeFromAWT.height
                    fill: Color.BLUE
                }
                Circle {
                    centerX: screenSizeFromAWT.width / 2
                    centerY: screenSizeFromAWT.height / 2
                    radius: if (screenSizeFromAWT.width <
                        screenSizeFromAWT.height) then
                        screenSizeFromAWT.width / 2
                        else screenSizeFromAWT.height / 2
                    fill: Color.RED
                    onMouseClicked: function(me: MouseEvent) {
                        FX.exit();
                    }
                }
            ]
        }
    }
}

We achieve the same effect as the first example by first making a call to a method in the java.awt.Toolkit package. With this information we can statically define our scenegraph without the use of binding.

There is one caveat to this solution. As the AWT (Advanced Windowing Toolkit) is an integral part of Java SE, this code should be portable across all JavaFX desktops. However, if you wish to deploy a JavaFX Mobile solution, the AWT calls would likely change. Is there a mechanism that might work across both models?

As a final thought, while we're on this theme of coining terms, my compadres Jim Clarke and Eric Bruno, co-authors of the aforementioned JavaFX book, jokingly asked what word could be used to describe this scenario:

"Condition where binds lead to binds that leads back to the original bind, ending up in a Stack fault?"

BindQuake? BindTsunami? Bindless? BindSpin? BindHole (BlackHole)? BindPit?

Jim Connors, a long-time member of Sun’s system engineering community, has spent a decade helping customers leverage Java technologies ranging from Java Card and Java ME to Java EE and JavaFX. His new book, co-written with Jim Clarke and Eric Bruno, is JavaFX: Developing Rich Internet Applications (also available in Safari Books Online and as a downloadable eBook.

Editor's Note: This article was previously posted on Jim Connor's blog and is

Copyright 1994-2009 Sun Microsystems, Inc. Reprinted with permission.

  • + Share This
  • 🔖 Save To Your Account