15 March 2015

JavaFX is a software platform for creating Rich Internet Applications that can run across variety of platforms[1].It has a collection of Java Packages with ability to add fancy GUI to your Java application. JavaFX is official standard part of Java platform. JavaFX is considered as a successor of Swing and can be easily understood by a Swing developer. You can get the latest in JavaFX in early access preview of JavaFX in Java 8 EA 40

History: Formerly it was called in F3(Form Follows function) and developed by Oliver Smith at Sun Microsystems. JavaFX was initially developed as a scripting language however it was later discontinued by Oracle and declared dead in Java One 2010. Now JavaFX has a clean Java API.

JavaFX Lingo In order to understand JavaFX well one needs to familiarize with the Jargon used

Stage: Stage is implicitly created by JavaFX. JavaFX makes you think like a designer and stage is where you will host your scene

Scene:A scene is a hierarchical node structures that contains all scene’s components

Group:Group is general purpose container node. This is analogous to a layout creates

Various possibilities with JavaFX

JavaFX Application Life Cycle
  1. init (): Default implementation of init does nothing. One can override to perform any processing before application UI is displayed
  2. start (): No default implementation and is declared abstract. Your app must override this. Build and display the UI
  3. Wait for application to End. Typically happens when user signals end of program
  4. stop(): Default implementation does not do anything One can override to to perform any necessary processing as program terminates
    • 
      
      package sampleapplication;
      
      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;
      
      /**
       *
       * @author rvvaidya
       */
      public class SampleApplication extends Application {
          
          @Override
          public void start(Stage primaryStage) {
              Button btn = new Button();
              btn.setText("Say 'Hello World'");
              btn.setOnAction(new EventHandler() {
                  
                  @Override
                  public void handle(ActionEvent event) {
                      System.out.println("Hello World!");
                  }
              });
              
              StackPane root = new StackPane();
              root.getChildren().add(btn);
              
              Scene scene = new Scene(root, 300, 250);
              
              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
          }
      
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              launch(args);
          }
          
      }
      
      
      
      

      Code Walk through

      • Any JavaFX application must extend javafx.application.Application
      • The start method is overriden. The override also passes the Stage object
      • Button object is instantiated and event handler is associated
      • A stack pane is created. This is a layout and corresponds to the group
      • Create the Scene object and pass the group reference
      • The scene is hosted on the stage
      • In main (the main entry point for java application) launch is called. This launches the JavaFX application.