Pedro Duque Vieira

About Pedro Duque Vieira

Pedro DV is a Software Engineer and self taught designer with 7 years experience in user interface/front end development. He's also the co-creator of Modellus, an hobby project.

Migrating from JavaFX 1.3 to JavaFX 2.0

Some days ago I finished migrating the source code of Modellus from JavaFX 1.3 script to JavaFX 2.0 java language. So I thought it would be nice to write about what I’ve learned in the process.

I’d like to point out that if you want to keep using JavaFX script in JavaFX 2.0 you can use Visage: http://code.google.com/p/visage/

  1. CustomNode class doesn’t exist any more. Extend Group or Region to create “custom nodes”.
  2. No more blocksMouse.
    In javafx 2.0 mouse events are only received by the top most node.
    There is also a new method on Node: setMouseTransparent(boolean). Mouse events on node with mouseTransparent set to true will be ignored and captured by the topmost node below.
  3. Use properties to bind values.
    Javafx 2.0 has a set of classes you can use to bind values to each other. For each primitive type there is a class – SimpleBooleanProperty, SimpleDoubleProperty, etc, and for reference types you use an Object Property instance, for instance if you want to bind colors you can use SimpleObjectProperty<Color>.
  4. Not all variables from the API are “bindable”.
    In Javafx 1.3 script you could bind to any variable of the API. In javafx 2.0 java language, that means that all variables from the API would need to be available as propertys. But that is not the case, for instance Bounds, LinearGradient, Stop are examples of classes that do not have propertys, so you can’t bind directly to their fields. In this situations you’ll need to use other methods like low-level binding.

    For example suppose you wanted to bind a variable to the width of the layout bounds of a node. Since the field width of Bounds is not available as a property you would have to do something like this:

    In Javafx script:

    float nameLabelXPosition = bind - nameLabel.layoutBounds.width / 2;
    
    

    In Javafx2.0 java language:

    nameLabelXPosition.bind(new DoubleBinding() {
    
        {
    
            super.bind(nameLabel.layoutBoundsProperty());
    
        }
    
        @Override
    
        protected double computeValue() {
    
            return nameLabel.getLayoutBounds().getWidth() / 2;
    
        }
    
    });
    
  5. When you used javafx script initiliazer blocks you can now use javafx builders.
    However in javafx script you could use binding in the initializer block, on java you can’t do that with builders. Only in JavaFX 3.0 (Lombard) will you be able to do that:http://javafx-jira.kenai.com/browse/RT-13680. So, whenever you used binding on javafx script initializer blocks you can’t use builders in java javafx 2.0.
  6. No more language level support for sequences on javafx 2.0 java.
    Wherever you used sequences you now will use ObservableLists. To create ObservableLists you can use FXColections creator methods, there you’ll find all sorts of methods to create ObservableLists, even empty ones.
    Sequences present on the API have been converted to ObservableLists. If, for instance, you want to insert a node on a Group you need to get it’s children ObservableList and than call the method add on it. Like so: .getChildren().add(Node)
  7. No more function types.
    Since only on java8 will there be support for Closures, the Oracle team has relied on the use of SAM types instead. That is a Class with only a single abstract method that you’ll have to override (Single Abstract Method). You can use the same strategy as Oracle and write SAM types wherever you used function objects.
  8. No more triggers.
    Replace triggers with change listeners. You can assign a change listener to a property which is the same as assign a trigger on javafx script.
  9. No more variable overrides on subclasses.
    For these one you won’t have a substitute on java, the best thing you can do is reassign a value to the variable on a subclass. But it is not the same, since overriding variables, assigned values before initializer blocks of superclass were invoked.

For further reading on this topic checkout:

http://weblogs.java.net/blog/opinali/archive/2011/05/28/javafx-20-beta-first-impressions

If you have any more valuable tips on this topic which I don’t cover please add them in the comments and I’ll insert them in the post.

Reference: Migrating from javafx 1.3 to javafx 2.0 from our JCG partner Pedro Duque Vieira at the Pixel Duke blog.

Related Articles :

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


one + = 3



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close