Questions & Answers

Changing background image with Button press Javafx

I want to change the background image from a scene.

This is from the css file.

.SettingsGrid {
    -fx-background-image: url('Background.quiz.jpg');
    /*-fx-background: #ffffff;*/
    -fx-background-size: cover;

I tried using this:

SettingsGrid.setStyle("-fx-background-image: url('Background2.quiz.jpg')");

But instead of the background changing to the new image, it just went white.

Just changing the background colour works.

2023-01-19 00:20:02
Read the uri documentation in the CSS reference. The resolution path is different depending on whether you access from a style sheet or an inline style. Try specifying the fully qualified path relative to the root of the classpath.
Answers(1) :

Did you make sure that your image is in the same directory as as the CSS file or the Java file where this code is being executed?

2023-01-19 00:20:02
Yes, all Background files, css and fxml files are in the same directory.