Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • 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 - Updated on December 10, 2022
in Java
Reading Time: 4 mins read
0
JavaFX Label Tutorial
385
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

Toggle
  • 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 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 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

YouTube video

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

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
216
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
588
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

JavaFX Toggle Button

Toggle Button in JavaFX | 100% Perfect for beginners

Leave a Reply Cancel reply

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

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

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

October 27, 2020 - Updated on January 23, 2023
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 July 3, 2022
JavaFX 17

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

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
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

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

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

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    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 Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. 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.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

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

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.