Engineering Full Stack Apps with Java and JavaScript
We will write a simple JavaFX application that displays a “Hello World” button. We will handle any button click events to print “Hello World” in the console. The program can be run from command line or from an IDE like eclipse, but without the Eclipse Plugin for JavaFX and Scene builder.
As of JDK 7 update 6, JavaFX is included with the standard JDK and JRE bundles. Hence you don’t have to download it separately if you have a JDK version higher than JDK 7 update 6.
You can use an IDE like eclipse, but is not a requirement and can use command line also.
To compile and run JavaFX applications using the command-line prompt or eclipse without plugin for JavaFX you will need to configure your CLASSPATH and include jfxrt.jar if it is not already there. As of JDK 7 update 6, it is available @ jre/lib folder.
Compile and execute the below program using command line or an IDE with jfxrt.jar in classpath. See ‘Beginning Java Book’ to see how to compile and execute java programs from command line.
package com.javajee;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class JavaFXHelloWorld extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
stage.setTitle("Hello World");
Group group = new Group();
Scene scene = new Scene(group, 200, 300);
Button btn = new Button();
btn.setLayoutX(50);
btn.setLayoutY(100);
btn.setText("Hello World");
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Hello World");
}
});
group.getChildren().add(btn);
stage.setScene(scene);
stage.show();
}
}
JavaFX applications extend the javafx.application.Application class. The entry point for JavaFX applications is the Application class. The Application class provides application life cycle functions such as launching and stopping JavaFX GUI components in a threadsafe manner.
According to the JavaFX documentation, the JavaFX runtime does the following, in order, whenever an application is launched:
The start method is abstract and must be overridden. The init and stop methods have concrete implementations that do nothing.
We have used below command to launch the JavaFX GUI:
Application.launch(args);
public static void launch(java.lang.String... args) method of Application launch a standalone application. This method is typically called from the main method(). The immediately enclosing class of the method that called launch will be launched passing the arguments passed as args. In this case we are not passing any args and hence it is same as:
Application.launch(null);
Calling Application.launch more than once will throw an exception like:
Exception in thread "main" java.lang.IllegalStateException: Application launch must not be called more than once…
The launched class must be a subclass of Application or a RuntimeException will be thrown like:
Exception in thread "main" java.lang.RuntimeException: Error: class com.javajee.JavaFXHelloWorld is not a subclass of javafx.application.Application…
In our example, the @Override annotation over start will fail if we are not extending Application. It is always a goof practice to use this annotation whenever you are overriding some method.
When the start() method is invoked, a JavaFX javafx.stage.Stage object is available for the developer to use and manipulate.
In JavaFX the Stage can be considered as the application window similar to Java Swing JFrame.
Scene can be considered as a content pane capable of holding UI controls and Shape objects.
Group can be considered as a container within a scene to hold a set of UI controls and Shape objects. We created and associated a Group to a scene:
Group group = new Group();
Scene scene = new Scene(group, 200, 300);
We then add ui controls and shapes, here button, to the Group:
Button btn = new Button();
…
group.getChildren().add(btn);
Next the scene is associated with the stage:
stage.setScene(scene);
And finally stage.show is called to display the application:
stage.show();
I also got the below warning, but it is just a warning and do not stop the application from displaying:
Device "Intel(R) HD Graphics" (\\.\DISPLAY1) initialization failed :
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302
I had another issue related to jar files as I had used 32 bit jars from java under Program Files (x86) on my 64 bit machine, and above warning had misled me for some time to think that the issue is due to some driver incompatibility. It was just a warning and did not stop the application from displaying.
Note: UI controls and Shape objects in JavaFX are direct or indirect children of javafx.scene.Node class. A Node is a fundamental base class for all scene graph nodes to be rendered. JavaFX 2.0 is based on a scene graph paradigm (retained mode) as opposed to the traditional immediate mode style rendering. JavaFX’s scene graph is a tree-like data structure that maintains vector-based graphic nodes. Similar to a tree data structure, a scene graph will contain children nodes by using a container class Group.
http://docs.oracle.com/javafx/2/api/javafx/application/Application.html