Troubleshooting JavaFX WebView parsing error when adding a stylesheet with calc function

Trying to embed a CSS file from a URL into a WebView with the following code:

this.webView.getStylesheets().add("http://page.com/style.css");

Unfortunately, JavaFX is generating errors:
CSS Error parsing http://page.com/style.css: Unexpected token '-' at [230,17]

Upon inspecting the file, it contains a 'calc' method:
width: calc(71% - 142px);
Is there a way to successfully load such a page? If so, how can this be achieved?

Answer №1

When styling a WebView, it's important to apply the stylesheet directly to the WebView itself and not the content it displays. This means that the CSS will affect the WebView itself and not the HTML content within. Keep in mind that JavaFX scene graph nodes do not support the calc function, so refer to the JavaFX CSS reference for valid CSS properties.

If your goal is to style the HTML document displayed by the WebView, you'll need to reference the stylesheet within the HTML document itself in the traditional way.

To load a stylesheet dynamically, you can manipulate the HTML document after it has been loaded. Consider the following approach:

private void addStylesheet(Document doc, String cssLocation) {
    Element docElement = doc.getDocumentElement();
    NodeList heads = docElement.getElementsByTagName("head");
    Element head;
    if (heads.getLength() == 0) {
        head = doc.createElement("head");
        docElement.appendChild(head);
    } else {
        head = (Element) heads.item(0);
    }
    Element link = doc.createElement("link");
    link.setAttribute("rel", "stylesheet");
    link.setAttribute("type", "text/css");
    link.setAttribute("href", cssLocation);
    head.appendChild(link);     
}

Then, implement the following:

WebEngine engine = webView.getEngine();

engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
    if (newState == Worker.State.SUCCEEDED) {
        addStylesheet(engine.getDocument(), "http://page.com/style.css");
    }
});

engine.load(/* your html ... */);

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 are the steps for displaying CKeditor's formatted text from a content management system on a Next.js website?

Presently, my method entails utilizing : <div dangerouslySetInnerHTML={{ __html: article.content }}></div> to display the formatted text retrieved from a CKEditor input field within a Strapi CMS backend. Is there an alternative approach to ach ...

Java program to retrieve characters from a string using their index position

I can't help but wonder why the number "13" is only printed once in this code snippet. Despite what IntelliJ is telling me about "i == 11 | i == 13" always being true, I just don't see how that makes any sense. for (int i = 0; i < 14; ...

Creating a responsive grid layout with HTML and CSS

Is there a way to adjust the grid layout so that the second row of blocks align just below the corresponding block above instead of creating an entirely new row? http://jsfiddle.net/AndyMP/wSvrN/ body { margin-left: 0px; margin-top: 0px; marg ...

Display the information contained within an array in a table using React

I have two arrays: one named formData and the other state array. const [formData, setFormData] = useState([]); let ure = [{}] useEffect(() => { axios .get("/api/listUre") .then((res) => { console.log(res.data ...

Display a custom toast from a list using Bootstrap 5

I've been utilizing the toast feature from Bootstrap, and it works perfectly when displaying a single toast. However, I encountered an issue when trying to display a specific toast, like an error toast, from a list of predefined toasts. It ended up sh ...

How to shift an image to the right side of the navbar

Is it possible to change the position of an image within a navbar from left to right? I have tried using the float property but it doesn't seem to work. .logo-img{ float: right; margin: 0px 15px 15px 0px; } <a class="navbar-brand logo-img" ...

When invoking the jQuery ".load('pageName')" method, HTML pages are not loaded from the application cache

I have been working on incorporating the html5 offline caching functionality into our html pages, and everything seems to be running smoothly with jQuery version 1.4. However, I encountered a problem when I upgraded to jQuery 1.8. Specifically, issues aro ...

Div text with superfluous line breaks

Whenever a user clicks the sign-up button ("Cadastrar" in Portuguese) on my website, the newsletter form successfully hides. However, there seems to be unnecessary line breaks in the success message. Why is this happening? I am trying to make sure that th ...

Adjust the color of the IText element on a Fabric.JS canvas (utilizing React and typescript)

I have an input with the type=color attribute positioned outside of a Canvas. Inside the canvas, there are one or more IText objects along with other elements. My goal is to "change the color of selected text objects when the input value changes". Incorpo ...

Grant permission for cross-domain access on the ADFS server

We are facing a specific scenario where we are using MS Teams Task Module to display and load a web page. This web page is connected with SSO (ADFS). Upon loading the web page in the popup, it redirects to the SSO link. The challenge we are currently enco ...

What is the best way to choose a particular user from the database and display only their information?

I am looking to create a table that displays user data from my database. In essence, I have set up an auto-increment "ID" field in my users' table and now I want to include a multiple-choice box where all the users in the database are listed. These u ...

What are the steps to make a Java program print using a printer?

What is the process for printing using a printer in Java? I am currently working on designing a board game for me and my friends to enjoy, however, the gameplay mechanics necessitate the creation of a new deck of cards for each match. ...

Simply install the classes JAR exclusively from a Maven WAR Project

My Maven project generates both a WAR file and a JAR file (with the classifier -classes, using the configuration <attachClasses>true</attachClasses> in the maven-war-plugin). Upon running mvn deploy, both of these artifacts are deployed to Mave ...

Techniques for extracting a specific section of a string in Javascript

On another web page, I am extracting a specific string. My goal is to store that string in a variable after a specific point. For example: #stringexample var variable; I need the variable to only include "stringexample" without the # symbol. How can I a ...

focusing on the child element of the current event's target

As I was working on a project, I came across a tab-menu jQuery plugin that needed some modifications. The issue I encountered was that the tabs were absolutely positioned, causing them to be taken out of the flow and not contribute to the wrapping div&apos ...

Sleek CSS3 Slide Menu - Smooth Transition to Top when Sliding

I have been experimenting with a CSS-only slide menu. Essentially, I set the nav menu position to fixed with top: 0 and right:-200 using radio buttons for control. <input type="radio" id="nav-expand" name="nav" /> <input type="radio" id="nav-col ...

Rendering of @font-face on Windows 7 was executed flawlessly

I have been utilizing @font-face to implement custom fonts on a website. The custom font displays correctly in Firefox, IE, Safari, and Chrome on Mac. However, when viewed on Chrome in Windows 7, the text appears green at 10px instead of black or grey. ... ...

Identify text truncation using JavaScript

I have a container with CSS that truncates text to show ellipsis when it reaches a certain width. Despite the truncation, the full text remains unchanged inside. To test this feature, I'd like to be able to access the truncated text with JavaScript. I ...

Error: Unable to locate requested resource

I have a scenario in Struts2 where I am utilizing an AJAX code snippet in the Script Section. var url="../scriptWay/myCallToBackEnd.ac?myToy="+myToyInfo; var respObj = ""; new Ajax.Request(url,{ ...

The radio button is displaying the text 'on' instead of its designated value

function perform_global(tablecounter) { for (index = 1; index <= 2; ++index) { var dnsname = "dns_name"+index; oRadio = document.getElementsByName(dnsname); alert (" radio ID " + dnsname + " " + index + "length " + oRadio.leng ...