JavaFX Label Example
In this section, we will be dealing with some examples related to labels in JavaFX.
Example1
In this example, we will show you how to create a simple text label using JavaFX.
Code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.text.TextAlignment;
public class App extends Application {
// launch application
public void start(Stage s) {
// set the title of the stage created
s.setTitle("Simple Text Label Example");
Label lb1 = new Label("This is a Coding Ninjas.");
// create a scene with (350 x 100) resolution
Scene sc = new Scene(lb1, 350, 100);
// finally host the scene inside the stage
s.setScene(sc);
// display the result
s.show();
}
public static void main(String args[]) {
// launch the application
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Explanation
Below is a simple explanation of the code:
- We first set the stage title inside the start method using the method setTitle().
- Then, We created a label using the Label(String s) constructor.
- We created a scene with the Label by setting the dimension as 350 x 100.
- Then, we added the scene to the stage.
- Finally, the show() method displays the result.
Example2
In this example, we will show you how to create a simple Image label with a test using JavaFX.
Code
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.image.*;
public class App extends Application {
// launch application
public void start(Stage s) {
// set the title of the stage created
s.setTitle("Simple Text Label Example");
try {
// Taking the image file from the appropriate location
FileInputStream inp = new FileInputStream("C:/Users/Aniket Majhi/Desktop/images.jpg");
// Constructing image from the input stream
Image img = new Image(inp);
// creating Node for the image
ImageView imgvw = new ImageView(img);
// Image Label with text
Label lb = new Label("This is Coding Ninjas", imgvw);
Scene sc = new Scene(lb, 400, 300);
// finally host the scene inside the stage
s.setScene(sc);
// display the result
s.show();
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
}
public static void main(String args[]) {
// launch the application
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Explanation
Below is the description of the above code:
- First, we took the image file from the specified location using FileInputStream().
- Then we constructed the image using Image() with the content of the input stream.
- Then, we created a Node for the Image using ImageView() class.
- We created an Image label with text using Label(String s, Node n).
- Then, We created a scene with the Label by setting the dimension as 400 x 300.
- Then, we added the scene to the stage.
- Finally, the show() method displays the result.
Frequently Asked Questions
How can we set change the text for a label in JavaFX?
We can change the text for a label using the setText() method. You can do it while the application is running.
How can we set the font for a label in JavaFX?
We can change the font for a label by calling the setFont() method in JavaFX. This method makes you change the size as well as the style of the text.
What is a Stage in JavaFX?
The JavaFX Stage is the top-level JavaFX container consisting of all the objects of a JavaFX application. The platform itself creates the primary stage. The Stage is under the javafx.stage and is represented by the javafx.stage.Stage class. To display the content of the stage, you need to call the show() method.
Conclusion
In this article, we have extensively discussed the JavaFX Label.
We started with the basic introduction. Then we discussed JavaFX Label, JavaFX Label Constructors, and JavaFX Label Methods. Finally, we showed some examples.
We hope that this blog gives you some ideas regarding JavaFX Label. If you would like to learn more, follow our articles on JavaFX. Explore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!
Do upvote this blog to help other ninjas grow.
Happy Reading!