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: