In this tutorial we will learn more JavaFX widgets usage via simple examples.
Example 1: Label
Let us look at the code
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following java files:
LabelChangeTextExample.java
LabelExample.java
LabelFontExample.java
- In your editor or IDE, create a file known as
LabelChangeTextExample.java
. - Then add the following code:
(a). LabelChangeTextExample.java
Like in every program we write, we will need to import functionalities into our project. Add the following imports:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Extend the class
as shown below:
public class LabelChangeTextExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
Let’s create a main method as shown below:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.label;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class LabelChangeTextExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Label label = new Label("Initial Label Text");
label.setText("New Label Text");
VBox vBox = new VBox(label);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX App");
primaryStage.show();
}
}
- Next create another file known as
LabelExample.java
. - And add the following code:
(b). LabelExample.java
Like in every program we write, we will need to import functionalities into our project. Add the following imports:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
Extend the class
as shown below:
public class LabelExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
Let’s create a main method as shown below:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.label;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class LabelExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Label label = new Label("Label Text");
VBox vBox = new VBox(label);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX App");
primaryStage.show();
}
}
- Next create another file known as
LabelFontExample.java
. - And add the following code:
(c). LabelFontExample.java
Like in every program we write, we will need to import functionalities into our project. Add the following imports:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
Extend the class
as shown below:
public class LabelFontExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
Let’s create a main method as shown below:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.label;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class LabelFontExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
Label label = new Label("A label with custom font set.");
label.setFont(new Font("Arial", 24));
VBox vBox = new VBox(label);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX App");
primaryStage.show();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |