JavaFX HelloWorld Command line Program without Eclipse Plugin & Scene builder

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.

 

Prerequisites

  • 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.

 

The JavaFX Program

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 HelloWorld Program Explanation

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:

  1. Constructs an instance of the specified Application class
  2. Calls the init() method
  3. Calls the start(javafx.stage.Stage) method
  4. Waits for the application to finish, which happens when either of the following occur:
    • the application calls Platform.exit()
    • the last window has been closed and the implicitExit attribute on Platform is true
  5. Calls the stop() method

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.

 

Reference

http://docs.oracle.com/javafx/2/api/javafx/application/Application.html

http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)