Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

JavaFX Label Tutorial | 100% Perfect for beginners

May 17, 2022
in Java
Reading Time: 4 mins read
0
JavaFX Label Tutorial
45
VIEWS
Share on FacebookShare on TwitterShare via Email

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.

Contents

  • JavaFX Label Tutorial with Examples
    • Create A Label
    • Display The Label
      • Output
    • Display an Image On The Label
      • Output
    • JavaFX Label Setter and Getter
    • Adding Style or CSS
      • Output
    • JavaFX Label Mnemonic Parsing
      • Output
    • Set Label Font
      • Output
  • YouTube Video

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

JavaFX Label Tutorial

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

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 in the JavaFX Label, If you don’t know the 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 Style

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

JavaFX Label Mnemonic

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));

Output

JavaFX Label Font

YouTube Video

JavaFX Label Tutorial | 100% Perfect For Beginners
Watch this video on YouTube.

Previous Post

JavaFX Button with examples | 100% Perfect for beginners

Next Post

How to use the JavaFX Hyperlink | 100% Perfect for beginner

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

JavaFX MenuButton
Java

How to use the JavaFX MenuButton | 100% Perfect Tutorial

June 19, 2022
17
How to use the JavaFX Hyperlink | 100% Perfect for beginner
Java

How to use the JavaFX Hyperlink | 100% Perfect for beginner

May 24, 2022
45
JavaFX Button
Java

JavaFX Button with examples | 100% Perfect for beginners

May 14, 2022
86
Next Post
How to use the JavaFX Hyperlink | 100% Perfect for beginner

How to use the JavaFX Hyperlink | 100% Perfect for beginner

JavaFX MenuButton

How to use the JavaFX MenuButton | 100% Perfect Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on November 28, 2021
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on April 30, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
change the stage icon in JavaFX

How to change the stage icon in JavaFX | 100% best for beginners

May 23, 2021 - Updated on October 29, 2021
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

send SMS in java

How to send SMS in Java | 100% best example

JavaFX MenuButton

How to use the JavaFX MenuButton | 100% Perfect Tutorial

June 19, 2022
How to use the JavaFX Hyperlink | 100% Perfect for beginner

How to use the JavaFX Hyperlink | 100% Perfect for beginner

May 24, 2022
JavaFX Label Tutorial

JavaFX Label Tutorial | 100% Perfect for beginners

May 17, 2022
JavaFX Button

JavaFX Button with examples | 100% Perfect for beginners

May 14, 2022

Latest Tutorials

JavaFX MenuButton

How to use the JavaFX MenuButton | 100% Perfect Tutorial

June 19, 2022
How to use the JavaFX Hyperlink | 100% Perfect for beginner

How to use the JavaFX Hyperlink | 100% Perfect for beginner

May 24, 2022
JavaFX Label Tutorial

JavaFX Label Tutorial | 100% Perfect for beginners

May 17, 2022

Popular Tutorials

  • Failed to automatically set up a JavaFX Platform

    Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube
Kensoft PH

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Website

Privacy Policy

Terms and Condition

Services

Guest Post

Fiverr

Freelancer

Upwork

Categories

Website Status

Check the status

Latest Tutorials

JavaFX MenuButton

How to use the JavaFX MenuButton | 100% Perfect Tutorial

June 19, 2022
How to use the JavaFX Hyperlink | 100% Perfect for beginner

How to use the JavaFX Hyperlink | 100% Perfect for beginner

May 24, 2022
JavaFX Label Tutorial

JavaFX Label Tutorial | 100% Perfect for beginners

May 17, 2022

© 2022 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
  • Java Quiz
  • Contact
  • About

© 2022 Made With Love By KENSOFT PH