FX Experience Has Gone Read-Only

I've been maintaining FX Experience for a really long time now, and I love hearing from people who enjoy my weekly links roundup. One thing I've noticed recently is that maintaining two sites (FX Experience and JonathanGiles.net) takes more time than ideal, and splits the audience up. Therefore, FX Experience will become read-only for new blog posts, but weekly posts will continue to be published on JonathanGiles.net. If you follow @FXExperience on Twitter, I suggest you also follow @JonathanGiles. This is not the end - just a consolidation of my online presence to make my life a little easier!

tl;dr: Follow me on Twitter and check for the latest news on JonathanGiles.net.

We’re intending to write a number of different smaller blog entries focused on describing various features and usages of JavaFX Script. I was asked several times by people this week at Devoxx, why choose JavaFX instead of HTML, Flex, or something else? The two real strong advantages of JavaFX are that 1) you have access to a wealth of Java APIs if you need it, and 2) Developing in JavaFX Script is highly productive.

It is this second point that I hope becomes clear and I hope to convey with this series of Language Lessons. Many times Jasper and I have been holed up writing demos with JavaFX and it is not uncommon for us to just pipe up every so often with “this is just so cool” or “javafx makes some things reaaaaally nice”. I know this sounds like the cheerleader squad coming from the guys who have been part of building this product. Call it pride in craftsmanship. Its honestly how we feel, and hope to convey this to you as we go along.

So with that, here’s the first Language Lesson: Object Creation.

There are actually two different syntaxes for creating objects in JavaFX. One should be used for creating Java objects, and the other for creating JavaFX objects. If you are going to create a Java Object, you will most often want to use the “new” keyword — exactly like you do in Java. You can either use the default no-arg constructor (if the class you are creating has one) or one of the other constructors. For those not familiar with Java, it looks like this:

def obj = new StringBuffer("Hello World");

The second syntax is what we call the Object Literal syntax, and looks more like object creation in JavaScript than in Java. JavaFX Script supports a different concept for object creation and initialization from Java. In the Java world, you have to call one of a fixed set of (generally) explicitly defined “constructors”, which are essentially special functions that create an object. Each constructor has certain parameters, just like functions.

This usually led to object creation & initialization code that looked like this:

JButton button = new JButton();

In JavaFX Script, there are no explicit constructors. Instead, you say what object you want to create and what initial values to give to each variable. If you don’t define an initial value for a variable, then the class will use its own initial value. For example, you might do this in JavaFX Script:

Button {
text: "Cancel"

There are some subtle differences between these two cases for a library author (like me), but it works really exactly the same as far as users of the library (like you) are concerned. The difference is that the traditional Java approach is very procedural (do this, then do that) whereas the FX way is declarative. In such simple examples as this it makes no difference, but as we will soon see as the Language Lessons continue, this declarative style lends itself to some very cool possibilities.

Until next time, have fun coding.