JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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