1

enter image description hereI create a button with -fx-graphic CSS,how to set -fx-graphic size in JavaFX?

I try to use -fx-max-height: 50; -fx-max-width: 50; but it didn't work.

the image is too large.

        Button deleteButton = new Button();
            deleteButton.getStyleClass().add("delete-button");
.delete-button {
    -fx-graphic: url(../view/Close.png);
    -fx-max-height: 50;
    -fx-max-width: 50;
}
Andy
  • 1,077
  • 1
  • 8
  • 20

1 Answers1

4

I don't think there is a way to manage your graphic size from CSS, but you can do it programmatically instead,

Button deleteButton = new Button();

ImageView image = new ImageView(new Image("../view/Close.png"));
image.setFitWidth(50);
image.setFitHeight(50);

deleteButton.setGraphic(image);

Alternatively,

You can set a background image on your button altenatively,

.deleteButton {
    -fx-background-image: url(../view/Close.png);
    -fx-background-size: 50px 50px;
    -fx-background-position: center;
    -fx-background-repeat: no-repeat;
}

Or

BackgroundSize size = new BackgroundSize(
        50,
        50,
        true,
        true,
        true,
        false);
BackgroundImage image = new BackgroundImage(new Image("../view/Close.png"),
        BackgroundRepeat.NO_REPEAT,
        BackgroundRepeat.NO_REPEAT,
        BackgroundPosition.CENTER,
        size);

deleteButton.setBackground(new Background(image));  
Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25