JavaFX Label create with both image and text

Introduction

To Create a label with both image and text in JavaFX

Label aLabel = new Label("OK", aImage);

Full source

// Demonstrate an image in a label.  

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Main extends Application {

  public static void main(String[] args) {

    launch(args);/*from   www .j  a  va  2  s  .co m*/
  }

  public void start(Stage myStage) {
    myStage.setTitle("Use an Image in a Label");

    // Use a FlowPane for the root node.
    FlowPane rootNode = new FlowPane();

    // Use center alignment.
    rootNode.setAlignment(Pos.CENTER);

    Scene myScene = new Scene(rootNode, 300, 200);

    myStage.setScene(myScene);

    // Create an ImageView that contains the specified image.
    ImageView aImage = new ImageView("your.png");

    // Create a label that contains both an image and text.
    Label aLabel = new Label("OK", aImage);

    // Add the label to the scene graph.
    rootNode.getChildren().add(aLabel);

    myStage.show();
  }
}



PreviousNext

Related