Tuesday, October 30th, 2007

Write your first JavaFX Script

Category: Java

Jim Weaver has posted a simple tutorial on writing your first JavaFX Script which discusses how you can use the JavaFX Pad to get it working.

  1. /*
  2.  *  HelloJFX.fx - A JavaFX Script "Hello World" style example
  3.  *
  4.  *  Developed 2007 by James L. Weaver (jim.weaver at jmentor dot com)
  5.  */
  6. package jfx_book;
  8. import javafx.ui.*;
  9. import javafx.ui.canvas.*;
  11. Frame {
  12.   title: "Hello World-style example for JavaFX Script"
  13.   height: 100
  14.   width: 400
  15.   content:
  16.     Canvas {
  17.       content:
  18.         Text {
  19.           font:
  20.             Font {
  21.               faceName: "Sans Serif"
  22.               style: BOLD
  23.               size: 24
  24.             }
  25.           x: 10
  26.           y: 10
  27.           content: "Hello JavaFX Script Developer!"
  28.         }
  29.     }
  30.   // Show the Frame on the screen
  31.   visible: true
  32. }
  33. // End of listing

Do you like the declarative code here?

Posted by Dion Almaer at 6:26 am

2.9 rating from 25 votes


Comments feed TrackBack URI

I personally miss the tags… You know.

Comment by Joao — October 30, 2007

How about a “Css” file for that thing? instead of x,y parameters and “style:” in the middle of the code?

Comment by Pedro — October 30, 2007

Whats wrong with HTML/CSS?

Comment by Diego Carrion — October 30, 2007

@Diego: Ha ha ha ha ha ha ha. Good one! Ha ha ha ha ha ha ha!

Comment by José Jeria — October 30, 2007


I *really* appreciate that you posted my blog entry to ajaxian. To address one of the commenter’s “x, y” comments, I’d just like to say that hardcoding x / y positions is just for Hello World-style code like in this tutorial. JavaFX Script has some very easy to use layout widgets that enable doing cross-platform UI layouts in a simple, declarative manner. It also has some powerful binding capabilities that allow you to bind attributes such as x / y positions to dynamic attributes in a model. I touched on the binding concept in a post already (see http://learnjavafx.typepad.com/weblog/2007/10/now-that-youve-.html), and will cover layout widgets in a future post.

Thanks again,
Jim Weaver
“JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications”

Comment by Jim W@eaver — October 30, 2007

No. No I don’t

Comment by Dan — October 30, 2007

>Do you like the declarative code here?

Not particularly. It seems like XML would make it a little easier to follow.

Is there an XML version of this? or is everyone expected to do this layout in code like that?

mike chambers

Comment by mike chambers — October 30, 2007


I’m no fan of layouts in code, but at least this syntax gives you some of the benefits of JSON in trade for being as brittle as code. XML is the worst of both worlds ;-)


PS: “Spam Question: What does the X in Ajax stand for?”. Heh.

Comment by Alex Russell — October 30, 2007


Personally, I prefer an XML based layout, with a DOM / parent / child API behind it.

That is why I was asking if there was an XML based version of the JavaFX stuff.

mike chambers


Comment by mike chambers — October 31, 2007

@mike chambers: I agree with you. XML is better for visual and human editors. FX Scripts adds another programmatic language to a complex architecture. What’s the benefit of FX Script? Why another programming language? Why not just use Java with XML templates?
I don’t like FX Script, and I will never join with it if I can.

Comment by Andrés Testi — October 31, 2007


There is not an XML based version of JavaFX Script syntax. As shown, it is very lightweight and JSON-like.

Jim Weaver

Comment by Jim W@eaver — October 31, 2007

Leave a comment

You must be logged in to post a comment.