Wednesday, September 2, 2015

JavaFX example: Label with image

JavaFX example to create Label with ImageView.


package javafx_imagelabel;

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;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageLabel extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        /*
        //Create Label with ImageView
        Label imageLabel = new Label(
                "java-buddy.blogspot.com", 
                new ImageView(new Image("http://goo.gl/kYEQl")));
        */

        //Create Label, then add graphic to it
        Label imageLabel = new Label("java-buddy.blogspot.com");
        imageLabel.setGraphic(new ImageView(new Image("http://goo.gl/kYEQl")));

        
        StackPane root = new StackPane();
        root.getChildren().add(imageLabel);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

No comments:

Post a Comment