Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

JavaFX 2.0 Developer's Guide

November 28, 2011

Steven Chin has created an excellent, thorough slide deck that illustrates with code how to develop JavaFX 2.0 applications. In the slides, there are sections for all of the controls, and how to populate them with data; how to use the various regions for layout; how to skin components to customize their appearance with CSS; developing custom controls and containers; binding; and various discussions on the internals of JavaFX.

Just as interesting are the discussions on JavaFX 2.0 development using other languages, such as Groovy, Scala, and yes, even JavaFX Script with Project Visage. This can be a very important topic for those with a significant investment in existing JavaFX Script code from their JavaFX 1.3.x work. It's also relevant since Visage continues the promise of the simplicity of JavaFX Script.

Although some people favor the newer Java APIs for JavaFX, many prefer these alternate languages as they allow you to get so much functionality out of relatively little code. Take these code snippets, for example.

Here is a simple JavaFX 2.0 application written in 19 lines of Java:

public class HelloWorld extends Application {	      
    public static void main(String[] args) {
        launch(args);	  	  	  
    }	  	  	  
    @Override public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Group root = new Group(); 
        Scene scene = new Scene(root, 400, 250, Color.ALICEBLUE);
        Text text = new Text();
        text.setX(105);
        text.setY(120);
        text.setFont(new Font(30));
        text.setText("Hello World");
        root.getChildren().add(text);
        primaryStage.setScene(scene);	
        primaryStage.show();
    }
}

Here is the same example written in 17 lines of GroovyFX:

GroovyFX.start { primaryStage -­‐>
    def sg = new SceneGraphBuilder()
    sg.stage(
        title: 'Hello World!,
        show: true) {
          scene(
          fill: aliceblue,
          width: 400,
          height: 250) {
            text(
              x: 105,
              y:  120,
              text: "Hello World!"
              font: "30pt")
         }
       }
}	   

And finally, the same example written in 14 lines of Visage-JavaFX Script:

Stage { 
    title: "Hello World!"
    width: 400
    height: 250
    scene: Scene {
      fill: BLUE
      content: Text {
        x: 105
        y: 120
        text: "Hello World!" 
        font: Font { size: 30pt }
      }
    }
}

For more information on developing the latest JavaFX 2.0 applications using various alternative languages, check out the following:

Happy coding!
-EJB

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video