×

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

Related Topics

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX HBox

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

3 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.