Javafx adding an icon to a button.





 import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class ButtonGraphicIcon extends Application
{
    public static void main(String[]args)
    {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage)
    {
        StackPane root = new StackPane();
        Scene scene = new Scene(root,600,400);
        //the camera icon to set on our button
        Image cameraIcon = new Image(getClass().getResourceAsStream("camera-128.png"));
        ImageView cameraIconView = new ImageView(cameraIcon);
        cameraIconView.setFitHeight(15);
        cameraIconView.setFitWidth(15);
        
        //create button
        Button btn = new Button("photo");
        btn.setGraphic(cameraIconView);//setting icon to button
        //setting button to stackpane
        root.getChildren().add(btn);
        
        primaryStage.setTitle("Button Icon");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above produces the following result:


Comments

Popular posts from this blog

Javafx adding icons to menu and menuitem.

Javafx inserting uploaded image into database.

Javafx displaying image from database.