JavaFX: Using Label with Graphics

As mentioned earlier, Label class is very similar to Text class and Label allows us to create a more expressive text display.

For example, we can put a graphics easily on the top, left etc of a jpeg file.

Label class provides us a setGraphics method and it also allows us to place the text on the different position around the graphics.

In the example below, I have put the label above the graphics by using


Take note also how I have accessed the jpg file using getResourceasStream method.  I have put the cloud.jpg in the same directory as

Label With Graphics
package javafxapplication13;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

public class JavaFXApplication13 extends Application {

    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        StackPane stackpane = new StackPane();

        Scene scene = new Scene(stackpane, 500, 300);

        Label lb = new Label("Stay Calm And Keep Learning");
        Image image = new Image(getClass().getResourceAsStream("cloud.jpg"));
        lb.setGraphic(new ImageView(image));
        lb.setFont(Font.font("Cambria", 24));


    public static void main(String[] args) {




Series Navigation<< JavaFX: Absolute Positioning Using Pane
JavaFX: Padding and Spacing >>