What is the proper way to apply styles to HTML by using an external CSS file?

Is it possible to fetch an HTML document online, present it in a JEditorPane, and format it using Java by incorporating external CSS files or <style>...</style> tags? Currently, I am utilizing jEditorPane.setPage(URL);, but the styling is not rendering properly.

Answer №1

According to the JavaDoc, jEditorPane is capable of supporting HTML 3.2 and CSS1, which are considered as the latest standards at that time. However, it is not recommended to use it for rendering modern web pages.

But you can still try the following workaround:

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);

URL url = new URL(location of your stylesheet);
StyleSheet styleSheet = new StyleSheet();
styleSheet.importStyleSheet(url)
kit.setStyleSheet(styleSheet);

Answer №2

It is not possible to display modern HTML using a JEditorPane. According to the documentation:

By default, the following content types are supported:

...

text/html

This refers to HTML text. The kit used in this case is the class

javax.swing.text.html.HTMLEditorKit
, which offers support for HTML 3.2.

The specification for HTML 3.2 dates back to the previous century and does not include CSS/CSS2.

To render HTML as we know it today, you would need to utilize an external library. A quick search online will unveil several options, or you can refer to resources like this one.

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

Any ideas on resolving this Selenium glitch: Microsoft Edge WebDriver version 118.0.2088.69 is starting, but only local connections on port 39280 are permitted?

I've researched extensively about this issue but still can't find a solution: Starting Microsoft Edge WebDriver 118.0.2088.69 (3cedde93c7077a64704badd2b4908bc2567d26ec) on port 39280 To give feedback, report a bug, or suggest new features, please ...

What is the best way to extract a single image from JSON data using AngularJS?

Using ng-repeat in HTML, how can I display only a single image instead of all images? <img class="size" ng-src="{{image.image}}" /> In CSS, I aim to define the size of the image. .size { margin-left:0.3em; width:11em; height:11em; border:0.4em sol ...

Translucent" outline for an "opaque" object

Consider the following HTML snippet: <div id="my_div"> </div> Now, let's take a look at the CSS code: #my_div { width: 100px; height: 100px; background-color: #0f0; op ...

The dropdown item in Tailwindcss is unexpectedly flying off the edge of the screen rather than appearing directly under the dropdown button

Currently, I am developing an application using Rails, Vue, and TailwindCss 1.0+ I am facing an issue while creating a dropdown menu for my products. When I click on the dropdown button, the items in the dropdown fly off to the edge of the screen instead ...

Grant permission for site A to access site B

These days, the commonly used term is "Single Sign On". Let me explain my situation. I apologize if I am using incorrect terms or if my explanation is unclear, as I am not an expert developer or architect. I have Site A (e.g., www.siteA.com) and Site B ...

What could be the reason for the Javascript function failing to run?

I need assistance in executing a function called "send()" which includes an AJAX request. This function is located in ajax.js (included in the code snippet) The Ajax success updates the src attribute of my image. The function seems to be working correctly ...

How can one extract collection type parameters (generics) from a JvmTypeReference?

While working in Xtext, my goal is to create a utility method that can retrieve the type parameter of a collection type represented by JvmTypeReference generated by the parser: @Inject TypeReferences typeReferences; public JvmType getCollectionType(JvmTyp ...

Enhance your website with Bootstrap 5's responsive rounded corners feature

Utilizing Bootstrap 5.3.1, I have successfully implemented responsive borders using the Utility API. My CSS file includes classes such as .rounded-top-sm-4 and .rounded-end-sm-4. # utilities_custom.scss @import "bootstrap/functions"; @import &quo ...

Utilizing HTMLUnitDriver with Selenium 3.0.x

In my Maven project, I am currently using selenium-java 3.0.1 and have added selenium-htmlunit-driver 2.52.0 separately to account for the lack of HTMLUnitDriver in this version. However, when running tests, I encounter the following exception: org.open ...

Transmit information through a socket and await a response

I am looking to implement the following steps: 1. Establish a connection to a UDP or TCP protocol. 2. Send data to a specified IP address. 3. Receive data from the specified IP address. 4. Store the received data in a variable. The task at hand involves ...

How can the 'Read more' feature be modified to impact multiple boxes? (Using jQuery, JS, and CSS)

I am trying to add a 'Read more' feature on my friend's website. I was able to achieve the desired effect, but when I tried to adjust the alignment of the box, it affected the 'Read more' feature. Original design: https://codepen. ...

Showcasing images in Flask

Hello everyone, I am encountering an issue with displaying images in my HTML file. Currently, I am using Flask within Visual Studio Code and here is the code snippet that I want to display: <img src="/templates/WANG_CHONG_st_1.png" width=" ...

Having trouble sending data to an API through jQuery: the function is not functioning properly

Currently, I am in the process of developing an HTML form that allows users to input values into text fields and submit them to an external API called ThingSpeak. This API then uses the received values to generate a plot displayed within an iframe. Althoug ...

Troubleshooting CSS Hover Not Displaying Properly in Angular 14

In the footer class of my HTML, there is a code snippet with chevrons: <div class="link-list"> <div *ngFor="let link of insideLinksLeft | originalOrderKeyValue" class="link"> <a [href]="link.val ...

"Exploring the possibilities of customizing Material UI tabs and implementing a tabs scroller

I'm currently trying to customize the appearance of these MUI tabs, specifically the tab color and bottom border color. Despite my attempts using makeStyles and other methods, I haven't been able to achieve the desired result. https://i.sstatic.n ...

Suggestions for displaying images of varying resolutions in a circular format similar to Google's style

What is the best way to display dynamic images with varying resolutions in a circle like Google? I have tried using the Bootstrap class "img-circle", but it only works for images with a resolution of 304*236. If the image resolution is different, it appear ...

Error message: Overlapping variable detected in Eclipse debugger

I've been grappling with this issue for quite some time now, but still can't seem to come up with a solution. private abstract class Node { private Rectangle bounds; } private class LeafNode extends Node { private Rectangle bounds; . ...

Tips for expanding the dimensions of a text box within the filter menu

In my application, I am using a kendo grid with filterable set to true. The filtering functionality is working properly. However, when I click on the filter symbol, the filter menu opens and I noticed that the size of the text-box within the menu is too sm ...

Customizing the primary CSS style of an element without the need to individually reset every property

Is there a way to reset just one property of a selector that has multiple properties stored in main.css, without listing all the properties and setting them to default values? I always struggle with CSS interference and constantly need to reset properties ...

Executing a PHP script

As I work on setting up a html form integrated with php for submitting user data to a database, I'm curious about the best tools to utilize in order to run and successfully test my program. Since it is a php file, what are the most efficient methods t ...