Home Course Index << Prev. PDF Version of this Page

Course API Comparison
Chapter C10: A JavaFX Application

Copyright © by S. Begic and V. Miszalok last update: 20-07-2009

For an introduction see: JavaFX and http://javafx.com.
Comparison JavaFX vs. Flash vs. Silverlight: by Terence Tsang

JavaFX is a Domain Specific Language DSL intended to create GUIs. It wraps Java's old fashioned Swing and Java 2D-APIs and simplifies their usage. The JavaFX-compiler just produces normal Java byte code and its result runs on any Java virtual machine.


Sun Microsystems offers two popular development environments to write JavaFX-code: NetBeans IDE 6.5.1 and JavaFX Plugin for Eclipse.
They are equivalent but both are far away from being luxurious GUI programming tools.
They offer a poor component library, no graphic editor and no layout manager.
Consequence: Up to now JavaFX cannot compete with Flash/Flex and Silverlight.

The following description is for NetBeans.
1) Download and install NetBeans IDE 6.5.1 Download jdk-6u14-javafx-1_2-windows-i586.exe → 118 MB.
2) Create a directory C:\temp\JavaFX.


Start NetBeans 6.5.1 IDE → File → New Project... → Choose File Type → Project: compareJavaFX Categories: JavaFX → Next → Name and Location → Class Name: compareJavaFX → Finish.
Open Main.fx and replace any existing code by:

package comparejavafx;

import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.control.TextBox;
import javafx.scene.transform.Scale;

var button1Width:Float  = 100;
var button2Width:Float  = 100;
var labelWidth  :Float  = 400;
var width       :Float  = button1Width + button2Width + labelWidth + 10;
var height      :Float  =  40;
var startText   = "JavaFx Application. Resize!";
var font        = Font { size: 18 }
var text        = startText;
var scene: Scene = bind Scene
{ width  :width
  height :height
var scaleW :Float = bind scene.width /width;
var scaleH :Float = bind scene.height/height;
var scale  :Float = bind if ( scaleW < scaleH ) then scaleW else scaleH;

def button1 = Button
{ text      :"Talk!"
  font      :bind font
  width     :bind button1Width;
  action    :function() { text = startText }
def button2 = Button
{ text      :"Clear"
  font      :bind font
  width     :bind button2Width;
  action    :function() { text = ""; }
def label = TextBox
{ text      :bind text
  font      :bind font
  width     :bind labelWidth;
  editable  :true
def hBox = HBox
{ content   :[button1, label, button2]
  transforms:bind [Scale.scale( scale, scale )]
  spacing   :5
def stage = Stage
{ title     :"JavaFX Resizable Text"
  scene     :scene

Run → Run Main Project F6.
NetBeans will create a C:\temp\JavaFX\compareJavaFX-directory containing the whole Project.
Sub-directory C:\temp\JavaFX\compareJavaFX\dist will contain the executable result compareJavaFX.jar.


Start compareJavaFX.jar without NetBeans

Unfortunatly *.jar-files do not start as *.exe-files do. They must be started by a tedious console JAR-launcher program.
The JavaFX-SDK which has been installed during the NetBeans IDE 6.7 installation contains such a JAR-launcher named javafx.exe.
It is hidden in a directory such as C:\Program Files\NetBeans\javafx2\javafx-sdk\bin.
The path can be different depending on your choices during the NetBeans installation dialogue.
Here is a batch program that you should copy via Notepad into an execute.bat file.
Then you must find out where the launcher javafx.exe and where the compareJavaFX.jar really are and you have to edit execute.bat accordingly.

cd C:\Program Files\NetBeans\javafx2\javafx-sdk\bin
javafx -jar C:\temp\JavaFX\compareJavaFX\dist\compareJavaFX.jar
cd C:\temp\JavaFX\compareJavaFX

After having adjusted the paths, store execute.bat to the project directory C:\temp\JavaFX\compareJavaFX.
Now you have to start the black console window.
Click the Windows-icon in the lower left corner of your screen.
Type cmd in the Start Search text field. The console window will open.
Now you have to switch the directory by typing cd C:\temp\JavaFX\compareJavaFX into the last line of the console.
After arriving in this directory type dir and check whether you detect execute.bat.
Type execute + return key.
The following out of NetBeans and out of browser application starts:

Resize it in any direction.
The fluid content is not very fluid. It follows quite slowly with some delay and looses a lot of resize events.
Bad performance seems to be a general problem of JavaFX programs.
Close the window JavaFX Resizable Text and you return to the console where you can start execute again.

 In order to start the program from the desktop, you have to create a shortcut of execute.bat on the desktop:
Windows Explorer → Computer → C:\temp\JavaFX\compareJavaFX → right click execute.bat → context menu → Send To → Desktop (create shortcut).
Whenever you click the shortcut, the console window will start first and (after a considerable delay) the window JavaFX Resizable Text follows.
top of page: