JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life cycle methods of the JavaFX application are provided by this Application class. Various Life cycle methods of JavaFX are:

  • public void init()
  • public void stop()
  • public abstract void start (Stage primarystage)

Also, we have to import javafx.application.Application class and inherit it in our class and have to override the start() method in our defined class.

Three main components of the JavaFX application are:

Scene:

All the physical nodes of the JavaFX application are contained in the Scene. All the methods interacting with scene object are available in javafx.scene.Scene class. If the user wants to visualize all the things on stage, we have to create the scene. The scene class object can be created and we have to pass the layout object to the Scene class constructor.

Nodes in Scene graph:

The scene graph is the collection of various nodes that exist at the last level of the javafx stack. Nodes are further seen on stage. This includes any components such as button, choice, text box, border or flow pane, image, etc. Always there are one root node and zero or more children nodes in the scene graph.

Stage:

We use the frame in swing applications as the basic container. In the same way, we use stage in JavaFX applications. It is the main container in which small nodes are placed. In the JavaFX, the application platform creates the Primary Stage. Then as per the requirements, new stages are created. In the start method, we have to pass the primary stage object. Then to display our stage we need to call the show method. This method is called with primary stage object. At the first time, the primary stage displays like an empty frame. Later we can add various components to it. Adding of these objects is done in a hierarchical way.

Stepwise procedure to create JavaFX application:

1. Import all the required packages and extend Application class and override start() method.

import javafx.application.Application;  
import javafx.stage.Stage;  
public class Demo extends Application{  
  
    @Override  
    public void start (Stage primaryStage) throws Exception 
   {  
          
    }  
  
}  

2. Create any UI component like button, text box, Choice or check box.

import javafx.application.Application;  
importjavafx.scene.control.Button;  
import javafx.stage.Stage;  


public class Demo extends Application
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  


         Button newbtn = new Button(" Submit ");  
          
    }  
  
}

3. Place the UI component on the layout.

import javafx.application.Application;  
import javafx.scene.control.Button;  
import javafx.stage.Stage;  
import javafx.scene.layout.StackPane;  


public class Demo extends Application
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  
        Button newbtn = new Button(" Submit ");  
        StackPane pane = new StackPane();  
        pane.getChildren().add(newbtn);  
          
    }  
}

4. Create the Scene.

import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Button;  
import javafx.stage.Stage;  
import javafx.scene.layout.StackPane;  


public class Demo extends Application
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  
        Button newbtn = new Button(" Submit ");  
        StackPane pane = new StackPane();  
        pane.getChildren().add(newbtn);  
        Scene newscene = new Scene(pane);      
    }  
  
}

5. After that create the Stage.

import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Button;  
import javafx.stage.Stage;  
import javafx.scene.layout.StackPane;  


public class Demo extends Application
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  
        Button newbtn = new Button(" Submit ");  
        StackPane pane = new StackPane();  
        pane.getChildren().add(newbtn);  
        Scene newscene = new Scene(pane);      
        primaryStage.setScene(newscene);  
        primaryStage.setTitle(" Demo JavaFX Application with Submit button  ");  
        primaryStage.show();  
    }  
  
}

6. Create the event for UI component.

import javafx.application.Application;  
import javafx.event.ActionEvent;  
import javafx.event.EventHandler;  
import javafx.scene.Scene;  
import javafx.scene.control.Button;  
import javafx.stage.Stage;  
import javafx.scene.layout.StackPane;  


public class Demo extends Application
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  
        Button newbtn = new Button(" Submit ");  
        newbtn.setOnAction(new EventHandler<ActionEvent>()
       {  
              
            @Override  
            public void handle(ActionEvent ae) 
           {  
                System.out.println(" Form submitted successfully ");  
            }  
        });  


        StackPane pane = new StackPane();  
        pane.getChildren().add(newbtn);  
        Scene newscene = new Scene(pane);      
        primaryStage.setScene(newscene);  
        primaryStage.setTitle(" Demo JavaFX Application with Submit button  ");  
        primaryStage.show();  
    }  
  
}

7. Create the main method and call launch method in it and pass command line arguments to it.

import javafx.application.Application;  
import javafx.event.ActionEvent;  
import javafx.event.EventHandler;  
import javafx.scene.Scene;  
import javafx.scene.control.Button;  
import javafx.stage.Stage;  
import javafx.scene.layout.StackPane;  


public class Demo extends Application
{  
  
    @Override  
    public void start(Stage primaryStage) throws Exception 
   {  
        Button newbtn = new Button(" Submit ");  
        newbtn.setOnAction(new EventHandler<ActionEvent>()
       {  
              
            @Override  
            public void handle(ActionEvent ae) 
           {  
                System.out.println(" Form submitted successfully ");  
            }  
        });  


        StackPane pane = new StackPane();  
        pane.getChildren().add(newbtn);  
        Scene newscene = new Scene(pane);      
        primaryStage.setScene(newscene);  
        primaryStage.setTitle(" Demo JavaFX Application with Submit button  ");  
        primaryStage.show();  
    }  


    public static void main (String[] args)  
    {  
        launch(args);  
    }  
  
} 

Output:

Basic Structure of JavaFX Application