Add image to a button at a specific position JavaFX


When I add image and text to a button, by default elements are set horizontally. How can I change this behavior to get text under image ?

10/1/2012 5:38:46 PM

Accepted Answer

Set the contentDisplayProperty on the button.


Here is an executable example:

import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonGraphicTest extends Application {
  @Override public void start(final Stage stage) throws Exception {
    final Label response = new Label();
    final ImageView imageView = new ImageView(
      new Image("")
    final Button button = new Button("I love you", imageView);
    button.setStyle("-fx-base: coral;");
    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent event) {
        response.setText("I love you too!");

    final VBox layout = new VBox(10);
    layout.getChildren().addAll(button, response);
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10; -fx-font-size: 20;");
    stage.setScene(new Scene(layout));;
  public static void main(String[] args) { launch(args); }
// icon license: (creative commons with attribution)
// icon artist attribution page: (eponas-deeway)

Sample program output

10/1/2012 9:57:16 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow