By
extending the abstract class 'javafx.application.Application', you can create a
JavaFX application. Your JavaFX application must override the start method of
Application class.
Find the
below example.
package com.sample.demos; import static javafx.geometry.Pos.CENTER; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.scene.text.Text; import javafx.stage.Stage; public class HelloWorld extends Application { @Override public void start(Stage primaryStage) { Button button = new Button(); button.setText("Click Me"); Text message = new Text(); button.setOnAction(event -> { message.setText("Hello World!!!!"); }); VBox vBox = new VBox(10, button, message); vBox.setAlignment(CENTER); Scene scene = new Scene(vBox, 300, 300); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("Hello JavaFX Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(HelloWorld.class, args); } }
When
you ran above application, you can able to see below window.
Click on the button ‘Click Me’, you can able to see “Hello World!!!!” message.
You can visualise above
application like below.
Click on the button ‘Click Me’, you can able to see “Hello World!!!!” message.
How it works
Application class is the entry point for all the JavaFX applications. Main class
of JavaFX application extends Application class and must provide implementation of 'start' method. 'start' method is the execution entry point for all the JavaFX
applications.
When the
application is launched, JavaFx application goes through below life cycle.
a.
Initialize the instance of specific Application class.
b. Calls
the init() method of the application.
c. Calls
the start method
d. Wait
until the application finishes.
e. Calls
the stop method.
let’s
analyze each class of HelloWorld application.
Stage and Scene classes
By using
Stage and Scene classes we can define user interface container. Stage is the
top-level JavaFX container, whereas Scene is the container for all content.
Platform constructs the primary stage. Application can construct additional
stages.
Button button = new Button();
button.setText("Click Me");
Above
statements construct a Button instance with text 'Click Me', it defines a node
to display button widget.
Text message = new Text();
Above
statement construct a Text object, it defines a node to display text.
button.setOnAction(event -> {
message.setText("Hello
World!!!!");
});
Whenever
you click on the button, it set the value "Hello World!!!!" to message
Text widget. I written this code using lambda expressions. If you would
like to know the lambda expressions, I would recommend you to go through my post.
VBox vBox = new VBox(10, button,
message);
vBox.setAlignment(CENTER);
VBox class
lays out the children in single vertical column. Above statement creates an
VBox layout with the horizantal spacing 10, between children (button and
message)
Scene scene = new Scene(vBox, 300,
300);
It is the
container for all content in a scene graph. By using scene, we can represent
all the visual elements of the user interface. A single element in a scene
graph is called node.
primaryStage.setTitle("Hello
JavaFX Example");
primaryStage.setScene(scene);
primaryStage.show();
Above
statements set the title of primaryStage, set the scene and display the stage.
No comments:
Post a Comment