Switching background images using a JavaFX Button click

I am attempting to modify the background image of a scene using CSS.

Here is the code snippet from the CSS file:

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

In an effort to change the background image, I tried the following:

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

Unfortunately, instead of displaying the new image as the background, it just turned white.

Interestingly, changing the background color does work successfully.

Answer №1

Make sure to provide the complete path starting from the root of the classpath.

The way to resolve the path differs depending on whether you are accessing a CSS URL resource within a style sheet or directly in an inline style.

Refer to the URI information in the CSS reference for detailed guidance and examples that can help clarify any confusion you may have. According to the CSS URL resolution documentation:

  1. If the address does not contain a scheme: part, then it is treated as just the path.
  2. A leading / at the beginning indicates that the path is relative to the classpath's root.
  3. In case the style is included in a stylesheet without a leading /, the path should be relative to the base URI of the stylesheet.
  4. For styles written directly in an inline format, the path is always relative to the classpath's root (regardless of the presence of a leading /).

For instance,

-fx-background-image: url('Background.quiz.jpg');

When utilized within a CSS file, this will search for the image alongside the stylesheet within the classpath.

However, when used in an inline style, it will look for the image at the classpath's root level.

To ensure correct resolution for the inline style URL, provide the full path starting from the classpath's root where the image resides.

If your application is packaged as a jar file, and the image is located in a place like image/Background2.quiz.jpg within the jar, then the appropriate URL to use in an inline style would be:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to efficiently pass a prop from a Parent styled component to its children's styled components, regardless of how deeply nested they are?

I am utilizing these customized components: import styled, { css } from 'styled-components' const Container = styled.div` background-color: ${props => props.theme === "light" ? "hsl(0, 0%, 98%)" : "hsl(235, 24%, 19% ...

How can I send an Array of parameters in JSON format to a SOAP web service?

I am trying to pass an Array of parameters to a Ksoap webservice in android using the following soap action. <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/ ...

What are the steps to correct a misread barcode during scanning?

Currently using the "me.dm7.barcodescanner: zxing: 1.9" library to extract barcode information. Although the program is functioning properly, there is one issue. The program is able to scan even when the barcode is not within the designated rectangle, resu ...

Unable to apply background-color CSS in playwright is not working as expected

I have been encountering an issue with applying the background-color CSS property in a Python template using the playwright package. I initially attempted to apply inline CSS style="background-color:red", then tried using a script tag with JavaScript, an ...

Unable to align only one link on the right as flex-end is not functioning correctly

I need help rearranging my row of items displayed at the top of the page. I want one item to appear on the far right while the rest are aligned to the left. I attempted using align-self: flex-end but it didn't work. Here's the current code snippe ...

Is there a way to alter multiple classes using hover effect on a single class without the use of JavaScript, only employing

Hello there! I have a question about applying styles to specific classes when hovering over certain elements. Unfortunately, I am unable to make changes to the HTML files and can only work with the CSS file. Take a look at the image below for reference: ht ...

Steps to resolve org.json.JSONException: Index 2 out of bounds [0..2) error

I am encountering an error when trying to retrieve data from the following JSON in my API response: {"success":true,"id":"4","name":"VOUCHER GOOGLE PLAY","data":[{"id":10,"product_id":"GLP","product_name":"GOOGLE PLAY","prefix":null,"pembeliankategori_id" ...

Creating a javascript function to update content on click

Recently, I've been designing a webpage and encountered an issue. I want the text in a specific area to change whenever a user clicks on a link. Below is the code snippet related to the section I want to modify using a JavaScript function. <div id ...

How can I make a div move to the position of another div and stay with it when the screen is resized?

In my card game project, I am dealing with an issue where cards are not aligning properly with the designated "dropZonePositions" when the screen is resized. Despite creating animations for the card movement that I'm satisfied with, the problem arises ...

Use a text link to upload a file instead of using an input field

While I've seen some discussions on this topic already, the conflicting answers have left me uncertain. Is there a foolproof method for triggering file upload without using an input field? Essentially, I'd like to create a div with an icon and te ...

Having trouble with button styling, unsure how to fix it

As someone who is new to learning, I am currently struggling with styling a button on my website. No matter what I try, it remains a white rectangle with just a border and some text. I have included the code for reference. Any help or advice would be great ...

- Utilize bullet points to exhibit keywords within a D3.js div by appending them

I'm looking to showcase the comma-separated values from a JSON file as a list in a mouseover tooltip on each node. Here is my current code snippet: div.append("div") .attr("class", "tooltip") .style("opacity", 1) .html("Node name : " + d.NodeName + ...

The images in my gallery refuse to hover or respond to my clicks

Ever since integrating this cropping effect (http://jsfiddle.net/BPEhD/2/) to my gallery images, I've encountered an issue - the hover state is missing and I can't click on the images either. Although the pointer cursor appears when I hover over ...

Issue encountered during the deserialization of a 2D JSON Array with Jackson Library

I am currently working on deserializing two different objects from JSON. The structure of the first object is as follows: String json = "[{\"name\":\"Random\"," + "\"coordinates\ ...

Buttons is having trouble with value initialization

Just diving into the world of Android and feeling a bit lost. Here's the current code snippet I'm working with: public class TypeActivity extends Activity { private boolean alcoholin = false; ... protected void onCreate(Bundle savedInstanceS ...

Utilize CSS to ensure divs display inline-block, but stack them only if they overlap

Below are the div elements I have: .parent { padding: 10px; background: grey; } .child { background: green; display: block; position: relative; width: 50px; } .stacked { left: 0px; } .three { left: 200px; } <div class="parent"&g ...

Guide to Extracting the Authorization Bearer Token from the DOM Layout in the Network Tab with Selenium Using Java

Seeking guidance on extracting the Authorization code from the DOM structure in the Network tab using Selenium with Java and then passing that token to the Rest Assured header as "Authorization", "Bearer". Any suggestions or help would be greatly apprecia ...

Convert Soundcloud thumbnail into a printable format using the @media print CSS rule

For those wanting to add an embedded Soundcloud player on a blog: <iframe width="500" height="300" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/271188615&color=%23ff5500&auto_p ...

What is the best way to round to the nearest 2.5 in Java?

Currently, I am in the process of developing a fitness app specifically for Android users. As part of this app, I would like to implement a feature where the user can input a number (e.g. 72.5) which will then be used to calculate percentages and apply var ...

Fancybox overlay not adjusting to full height

When working with JavaScript: $(document).ready(function() { $(".fancybox").fancybox({ helpers: { overlay: { opacity: 0.4, css: { 'background-color': '#FFF' ...