0

Essentially, what I want is to create a Button with a cropped graphic. Cropped in the sense of, the Image is in the back, and the Button is a hole, showing the Graphic. As of now, it looks likeFailed Attempt

However I want the graphic to fit the button, even if its bigger, to just get cut off. My current code is along the lines of

Image image = new Image(Main.class.getResource("/texture.png").toExternalForm());
yesButton.setGraphic(new ImageView(image));
VeeAyeInIn
  • 193
  • 1
  • 12

1 Answers1

4

You can set a background using javafx css:

Button button = new Button("Button");
button.setStyle("-fx-background-image: url('/texture.png')");

Or programmatically with Background by setBackground:

Image image = new Image(Main.class.getResource("/texture.png").toExternalForm());
BackgroundImage backgroundImage = new BackgroundImage(image, BackgroundRepeat.NO_REPEAT,
        BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.AUTO);
Background background = new Background(backgroundImage);

Button button = new Button("Button");
button.setBackground(background);
dzikoysk
  • 1,560
  • 1
  • 15
  • 27
  • Is there any way to preserve the shape of the button? I.e. the curve instead of the sharp edges from the images? – VeeAyeInIn Mar 24 '18 at 21:06
  • You should look at questions with border styling issues, for instance: https://stackoverflow.com/a/20490028/3426515 or https://stackoverflow.com/q/43557722/3426515 – dzikoysk Mar 24 '18 at 21:20