×

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 Pause Transition

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

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 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 Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

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

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 Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 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 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 Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 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 ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Rotate Transition

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

4 minutes read.