Java, Eclipse and Web programming Tutorials

JavaFX development with Eclipse - Tutorial

Lars Vogel

Version 0.5

19.10.2009

Revision History
Revision 0.114.12.2008Lars Vogel
First Version
Revision 0.210.01.2009Lars Vogel
Update article to reflect fixed bugs in the JavaFX plugin
Revision 0.302.06.2009Lars Vogel
Upgrade to JavaFX 1.2 and the new Eclipse Plugin
Revision 0.418.10.2009Lars Vogel
Minor re-work
Revision 0.519.10.2009Lars Vogel
Added timeline

JavaFX with Eclipse

This article describes how to develop your JavaFX application using Eclipse.

This article is based on JavaFX 1.2, Java 1.6 and Eclipse 3.5 (Galileo). This article assumes that you know how to use Eclipse for creating Java programs.


Table of Contents

1. JavaFX Script
1.1. Overview
1.2. JavaFX UI application
2. Installation
2.1. Installation of JavaFX
2.2. Installation of the JavaFX plugin
3. First JavaFX Script Project
3.1. Create Java Project
3.2. Code
3.3. Run
3.4. Adding Timeline, Events and binding
4. Slider Demo
5. Thank you
6. Questions and Discussion
7. Links and Literature

1.  JavaFX Script

1.1. Overview

JavaFX is a rich client platform for building cross-device applications and content. This article will use JavaFX script, the programming language for JavaFX. The following will use JavaFX as a short term for JavaFX script.

JavaFX script is a programming language from Sun for creating rich media and interactive content which is tightly integrated with Java. With JavaFX it should be easy to develop Rich Internet Applications (RIA).

It is a statically typed language which means that the data types of each variable, return value, etc. is known at compile time. JavaFX uses type inference / implicit typing to identify the type of a variable, e.g. the programmer does not have to specify the type himself. JavaFX is a declarative language, e.g. you describe what the application should do, rather then specifying the control flow of the application yourself. See Declarative programming for details.

The language provides data binding to connect UI elements with model elements.

JavaFX is compiled to Java byte code.

1.2. JavaFX UI application

The basis for a JavaFX UI application is the class javafx.stage.Stage. Within this stage you have a hierarchy of graphical elements which are described as part of a javafx.scene.Scene. These graphical elements are called nodes and can be:

  • JavaFX component, e.g. Image, Rectangle, Text, MediaView or a wrapped Swing component

  • JavaFX class (custom node)

  • Group: Container for other nodes

2.  Installation

This article assumes that you install Eclipse and have basic knowledge of using Eclipse. Please see Using the Eclipse IDE for an introduction to Eclipse.

2.1.  Installation of JavaFX

Download JavaFX SDK (not the Netbeans bundled version) from http://java.sun.com/javafx/downloads .

JavaFX comes with an automatic installation routine for Windows, for the other platforms please check the installation guides of Sun.

2.2. Installation of the JavaFX plugin

Use the Eclipse update manager to install the JavaFX plugin from http://javafx.com/downloads/eclipse-plugin .

See Using the Eclipse Update Manager for additional information.

3. First JavaFX Script Project

3.1. Create Java Project

Create a new JavaFX project "de.vogella.javafx.first" via selection File -> New -> Project -> JavaFX

Select the empty project and press finish.

3.2. Code

Create a package "de.vogella.javafx.first". Right click on the package, select New -> Empty JavaFX Script

Call your new script "MyScript"

Note that the JavaFX perspective offers nice snippets which you can use to build for JavaFX application. Open "Applications" and select Stage.

Drag "Stage" into your source editor window. Change the title to "My first JavaFX program". Press then insert.

Change the code to the following.

				
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.*;
import javafx.scene.shape.Circle;

package de.vogella.javafx.first;


/**
 * @author Lars Vogel
 */
Stage {
    title : "My first JavaFX program"
    scene: Scene {
        width: 200
        height: 200
        content: [ 
         Text {
              font: Font { 
              		size: 12 
              		}
              x: 10, 
              y: 50
              content:"Hello Java FX World!"
              }
         Circle {
                centerX: 100,
              	centerY: 100,
                radius: 40,
                fill: javafx.scene.paint.Color.BLACK
              	}
        
        ]
    }
}
			

Tip

The Eclipse plugin does not yet support auto import.

Tip

Auto formating of the Eclipse seems not be working.

3.3. Run

Select your script, right mouse click on it and select "Run as -> JavaFX Application"

Here you can define how your program should run. Under Profile select "Desktop profile - Run as Application". Press run.

The result should look like the following.

Congratulations. You created your first JavaFX application.

3.4.  Adding Timeline, Events and binding

Change the code to the following to make usage of a timeline which changes the size of the ball. The animation is activated by clicking on the circle.

				
package de.vogella.javafx.first;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.text.*;
import javafx.scene.shape.Circle;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.input.*;
import javafx.animation.*;
import javafx.scene.*;
import javafx.animation.transition.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.transform.*;
import javafx.stage.*;


/**
 * @author Lars Vogel
 */
var size = 30 ; 
var time =  Timeline {
     			repeatCount: Timeline.INDEFINITE,
                keyFrames: [
                 KeyFrame {time: 0s  values: size=> 20 },
                 KeyFrame {time: 5s  values: size=> 100 tween  Interpolator.LINEAR }
   				]
                       
			}
			
Stage {
      
    title : "My first JavaFX program"
    scene: Scene {
        width: 200
        height: 200
        content: [ 
         Text {
              font: Font { 
              		size: 12 
              		}
              x: 10, 
              y: 50
              content:"Hello Java FX World!"
              }
         Circle {
                centerX: 100,
              	centerY: 100,
              	
                radius: bind size,
                fill: javafx.scene.paint.Color.BLACK
                onMousePressed: function(e:MouseEvent) {
                        time.play();
                    }
              	}
        ]
         
         
    }
  
}
			

4. Slider Demo

Create a new Java project "de.vogella.javafx.slider". Use the template "Display Self".

Select in the package "displayself" the file "Main.fx" and run it as application.

5. Thank you

Thank you for practicing with this tutorial.

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

6. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.

I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.

7. Links and Literature

http://java.sun.com/javafx JavaFX homepage

http://kenai.com/projects/eplugin JavaFX Plugin for Eclipse Homepage

http://www.javafx.com/samples/ JavaFX examples

http://www.javafx.com/samples/Tweeter/ JavaFX Twitter Client example