×

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on a container to display text on the screen. It is mainly used to give instruction or information to the user. Various constructors in javafx.scene.control package for Label are:

  1. Label(): This constructor is used to create label without any text.
  2. Label(String newtext): This constructor is used to create a label with the text entered by the user.
  3. Label(String newtext, Node graphic): This constructor is used to create a Label with user text and graphics.

Example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;  


public class SimpleLabel extends Application
{  
@Override  
public void start(Stage primaryStage) throws Exception
{  
    Label newlb = new Label(" Label in JavaFX ");  
    StackPane pane = new StackPane();
    Scene scene = new Scene(pane,500,400);  
    pane.getChildren().addAll(newlb);  
    primaryStage.setScene(scene);  
    primaryStage.setTitle("Label Example in JavaFX ");  
    primaryStage.show();  
}  


public static void main(String[] args) 
{  
    launch(args);  
      
}  
}

Output:

The java file is created which extends the Application class. Also start method is overridden with a primarystage object as an argument in it. In the start method label is created with text specified in it. Then StackPane container is created. An object of StackPane is passed to Scene constructor as an argument.. Then addAll() method is then added to the container. Using show() method on primaryStage is called to display container. Then in main method launch(args) method is called to launch the application.

Right click on the java file, to run the application. Then select Run As, and choose Java application. It will show one container with the title “Label Example in JavaFX” and Label in the center as “Label in JavaFX”.

JavaFX UI Control - Label

Displaying Graphics with Label:

We can also display the image in Label. In JavaFX, we are allowed to display graphics next to the label. Using constructor, we can specify graphics with the label.

Example:

import java.io.FileInputStream;  
import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Label;  
import javafx.scene.image.Image;  
import javafx.scene.image.ImageView;  
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
  
public class GraphicsText extends Application 
{  


  
    @Override  
    public void start(Stage primaryStage) throws Exception
   {   
          
        StackPane pane = new StackPane();   
        FileInputStream file = new FileInputStream ("home.jpg");  
        Image newimage = new Image(file);  
        ImageView imagev1 = new ImageView(newimage);  
        Label my_label  = new Label(" Visit Homepage ",imagev1);  
        Scene scene = new Scene(pane,600,400);  
        pane.getChildren().add(my_label);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("Label Class Example with home graphics icon ");  
        primaryStage.show();  
          
    }  
    public static void main(String[] args) 
   {  
        launch(args);  
    } 
 
}

Output:

The java file is created which extends the Application class. Also start method is overridden with a primarystage object as an argument in it. The StackPane object is created in start(). Also, the FileInputStream object is created using a constructor and specifying the file path. Image object is created and then it is passed in the ImageView constructor. The label constructor is created by passing text and image arguments to it. The scene object is created then the stage is prepared. The title is set to the primary stage and then using the show() method it is displayed. In main method launch(args) method is called to run the application.

Right click on the java file, to run the application. Select Run As, and choose Java application. It will show one container with the title "Label class Example with home graphics icon” and Label in the center as “ Visit homepage” with a home icon.

JavaFX UI Control - Label

Related Topics

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.