Peter Penzov
Peter Penzov

Reputation: 1678

Set StackPane background image

I want to set background image autoresized with StackPane.

I tried this:

StackPane stackPane = new StackPane();
Image im = new Image(Dashboardpanel.class.getResource("/images/11.jpg").toExternalForm()); 
stackPane.setBackground(new Background(new BackgroundImage(im, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));

But the image is not auto-resized when I resize the StackPane. The image is repeated to fill the gap. How I can resize the image just as the Stackpane no matter how I extend the StackPane?

Upvotes: 1

Views: 4100

Answers (1)

jewelsea
jewelsea

Reputation: 159331

Don't use the default BackgroundSize, create a new BackgroundSize and use the contain and cover arguments of the BackgroundSize constructor.

For example (and I just wrote this by reading the javadoc, I didn't test it):

new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, false, true);

I advise using a CSS stylesheet for this kind of styling rather than coding the style in Java.

Upvotes: 2

Related Questions