JavaFX – Performance Optimization

Bookmark and Share

There was a question related to Best Practices for speed optimization in Oracle-Mix forum. There are many small tips & tricks, just thought of listing out what ever came to my mind..

Use Layout: For positioning node always use Layout. There are plenty of layout available such as HBox, VBox, Tile, Flow and Stack. If we need any custom layout you can use Container or Panel. Position node using layoutX and layoutY attributes. Use translateX and translateY for Animation.

Consolidate Theme Attributes : We can reuse many theme related objects such as Font, Color, Paint, Image. Eg: If we use same Font for many Label/Text, we can use the same Font instance instead of creating one each for each instance of Node. This consolidation also allow us to customize the UI. You may refer to Theme used in Calendar sample. Also share Image instances such as default-image, place-holder image etc across multiple objects.

Bind with Caution: I have stressed on this in one of my previous post. Repeating the same again. Use this mostly for wiring model to view. Minimize use for other purpose such as layout of Nodes. Also better to avoid chaining of binds, its very problematic and difficult to debug.

JavaFX for View: There is a tendency to write entire application using pure JavaFX. This may be fine for small widget like application or samples. But when you write real business application we need to push out the business logic, IO operations and non-ui related code to Java layer. Use JavaFX Async APIs + Callback methods so as to execute these code outside Event-Dispatch-Thread. HttpRequest API demonstrates use of this approach.

Reduce use of Effects: In many cases we may need to have static effects in UI, such as nice gradient for background, font effects etc. If these effects are static, its always better to use Image instead of effect API.

Stroke Vs Fill: Instead of using stroke attribute for shape, we may use nested shapes with fill attribute. Example: For creating a stroke for Rectangle, we can create two instances of Rectangle overlapping, with the inner Rectangle smaller than outer Rectangle. This will have better performance compared to use of stroke attribute.

Avoid Class-Path in Manifest: When we add various jars as library to Netbeans, these jars are directly referenced in Class-Path of Manifest file of the application jar. When deployed, this will force to download all jars before start of application. Concept of lazy download will not work. Also there will be an attempt to make an unnecessary network connection, even though resources such as plugin.jar, javaws.jar etc are not hosted. This will increase initial applet/javaws startup time.

Use Image.width & Image.height: If we need to show a thumbnail of large image, its better to specify its width and height Image object itself rather than resizing the ImageView using fitWidth and fitHeight attribute. This will reduce the memory used to hold the image.

Provide Startup Feedback: Many a times people get bored due to lack of proper feedback during startup. This creates a notion of increased startup time. Except for small use-cases such as advertisements, RIA’s do have some startup time. It will be nice to provide a Custom Splash Screen and even better a Progress Listener UI to give useful feedback.

Tips for Mobile: : Joshua Marinacci has listed some useful tips for mobile.

I’m sure there are more… ‘ll keep updating this list… 🙂


About Rakesh Menon

13 Responses to JavaFX – Performance Optimization

  1. Pingback: uberVU - social comments

  2. Rakesh Menon says:

    @William & Jim Thanks!

  3. Bob Foster says:

    If you can’t chain binds, what good are they?

    • Rakesh Menon says:

      @Bob Foster These are general guidelines for JavaFX 1.2, things have improved in 1.3. I’ll wait for the release and then update the list. Many have used this before and found it very difficult to debug. May be we need to provide some guideline on actual usage related to chaining.

  4. Pingback: JavaFX links of the week, March 1 // JavaFX News, Demos and Insight // FX Experience

  5. Pingback: Java desktop links of the week, March 1 | Jonathan Giles

  6. alexsentcha says:

    Hi Rakesh,

    package …
    import vis.Class1;
    import vis.Class2;

    var name1 = Class1;
    var name2 = Class2;

    but I need array?

    var name: ??= [name1, name2];


    var name;
    if(condition1) {name = name1; } else { name = name2}

    Do you now? is that possible?

    • Rakesh Menon says:

      Sorry, I didn’t understand the objective here… var name1 = Class1; Its not instantiating Class1, so it won’t compile right?
      If there are no common ancestors for Class1, Class2, you will have to create an Object[]

      Or are you trying to create a sequence of Class? You can create it as shown below..

      var name:java.lang.Class[] = [ Text.class, Button.class ];

  7. Alex says:

    Hi rakesh
    small question
    Can i put classes in array ?

  8. Alex says:

    Hi Rakesh
    I mean:
    var elementarray: javafx.scene.Node[];

    thank you, that work well.

  9. Pingback: JavaFX - Otimização da Performance

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: