JavaFX Label control is very useful when you are developing an application. In an application, for example, a label control would be required to tell users that this text field is for “First name”, or “Last name”. There is nothing special in label control, but you should learn the basics like how to create the JavaFX Label, mnemonic parsing, adding an image to the label, setter, and getter, etc.
JavaFX Label Tutorial with Examples
In this JavaFX Label tutorial, you will learn the basic and essential use of label control. So, the label class represents label control, and the use of this control is simply a label used to tell the users to describe or identify nodes in the computer application. The label control does have the ability to display a text, an icon, or both. The first step in this tutorial is to create the label in JavaFX.
Make sure you are familiar with the layout in JavaFX. Click the link to learn more about it. I will provide codes and output, so you can easily copy the codes if you want and see the output on what the code looks like. Please proceed below to learn more and let’s get started.
Create A Label
To create a label in JavaFX, you need to make a label object or instantiate the label class. In this example, we will create a label by passing a text on the label constructor.
Label label = new Label("Java programming");
Display The Label
To display the JavaFX label in your application, you need to add the label in your JavaFX layout or add it directly on the scene. In this example, I will provide two options for displaying the label in your application.
// Displaying the label with layout StackPane root = new StackPane(); Scene scene = new Scene(root, 300, 100); Label label = new Label("Java Programming"); root.getChildren().add(label); stage.setScene(scene); stage.setTitle("Understanding Label"); stage.show(); // Directly add to the Scene Label label = new Label("Java Programming"); Scene scene = new Scene(label, 100, 100); stage.setScene(scene); stage.setTitle("Understanding Label"); stage.show();
Output
Display an Image On The Label
Displaying an image on the label is very easy to do. We need to use the setGraphic() method, but we also need ImageView to achieve our goal of displaying the image inside the label. Please proceed below to see how it works.
// Displaying the label with layout StackPane root = new StackPane(); Scene scene = new Scene(root, 300, 100); Label label = new Label("Java Programming"); root.getChildren().add(label); // Display an image ImageView icon = new ImageView("/res/JavaIcon.png"); //res is the folder from my project label.setGraphic(icon); stage.setScene(scene); stage.setTitle("Understanding Label"); stage.show();
Output
JavaFX Label Setter and Getter
The setter and getter are useful if you want to change the text in the label, and the getter is used to get the label text. Please proceed below to learn more.
//Change the Label Text (Setter) label.setText("Learning with Kensoft PH"); //Getting the Label Text System.out.println(label.getText());
Adding Style or CSS
It is possible to add styles to the JavaFX Label. If you don’t know JavaFX CSS yet, click the link to learn more. Adding CSS to our application or nodes will make it look better. In this example, I will show you how to add styles to the label using the inline style method. Please proceed below to learn more.
// Displaying the label with layout StackPane root = new StackPane(); Scene scene = new Scene(root, 300, 100); Label label = new Label("Java Programming"); root.getChildren().add(label); // Display an image ImageView icon = new ImageView("/res/JavaIcon.png"); //res is the folder from my project label.setGraphic(icon); // Adding Style label.setStyle("-fx-font-size: 20px; -fx-text-fill: red;"); stage.setScene(scene); stage.setTitle("Understanding Label"); stage.show();
Output
JavaFX Label Mnemonic Parsing
The use of mnemonic is similar to focus, Mnemonic parsing for label is set to false by default. Press the mnemonic key or press ALT to activate the mnemonic for label. The following example below will show you enabling the mnemonic in JavaFX Label.
// Displaying the label with layout StackPane root = new StackPane(); Scene scene = new Scene(root, 300, 100); Label label = new Label("_Java Programming");// You need to add an underscore before the text root.getChildren().add(label); // Display an image ImageView icon = new ImageView("/res/JavaIcon.png"); //res is the folder from my project label.setGraphic(icon); // Adding Style label.setStyle("-fx-font-size: 20px; -fx-text-fill: red;"); // Enabling Mnemonic Parsing label.setMnemonicParsing(true); stage.setScene(scene); stage.setTitle("Understanding Label"); stage.show();
Output
Set Label Font
Changing the font is also possible in style, but in this example, we are going to change the font without using the style. Please proceed below to learn more.
//Change the font label.setFont(new Font("Arial Black", 20));