JavaFX Tutorial - Java Label(java.lang.String text, Node graphic) Constructor








Syntax

Label(java.lang.String text, Node graphic) constructor from Label has the following syntax.

public Label(java.lang.String text,
  Node graphic)

Example

In the following code shows how to use Label.Label(java.lang.String text, Node graphic) constructor.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/*from w  w w.  j a va 2  s .  c  o m*/
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();
    Image image = new Image(getClass().getResourceAsStream("labels.jpg"));
    Label label1 = new Label("Search",new ImageView(image));


     hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

    stage.setScene(scene);
    stage.show();
  }
}