Reputation: 68
I know that Java Swing's HTML renderer is super basic and very much outdated (and I guess has not received any updates since it was added, which is sad tbh), but I use it to display a very simple HTML that consists of unordered lists and some <code>
tags. The problem is that I am able to change the background of code tags using CSS styles, but border-radius
attribute is not implemented. How can I paint rounded border around my code tags so they look nice? I also tried digging into javax.swing.text.*
package, but the code seems to be very abstract and I don't see where the magic happens.
Sample:
StringBuilder fullHtml = new StringBuilder();
fullHtml.append("<html>");
fullHtml.append("<head>");
fullHtml.append("<style>");
fullHtml.append("code {");
fullHtml.append("background: ").append(ColorUtils.colorToHex(UIManager.getColor("InstanceItem.defaultColor"))).append(";")
// this line has no effect
.append("border-radius: 10px;");
fullHtml.append("}");
fullHtml.append("</head>");
fullHtml.append("<body>");
fullHtml.append(html);
fullHtml.append("</body>");
fullHtml.append("</html>");
Basically I would like it to have the same shape as on Stackoverflow.
If you have any other suggestions for maybe a better renderer or a completely different technique to display the data, feel free to tell me. I know about a thing called CSSBox but it does not seem to have border-radius
implemented as well.
Upvotes: 2
Views: 47
Reputation: 9459
There seems to be some capability in JavaFX (which is not core Java Swing but maybe combinable)?
https://codingtechroom.com/question/display-html5-in-swing
Essentially the page says: Displaying HTML5 content in a Java Swing application can be accomplished using various components. Although Swing doesn’t natively support HTML5 rendering, you can use JEditorPane for basic HTML or integrate JavaFX WebView for full HTML5 functionality.
Their code example looks like this:
import javax.swing.*;
import javafx.application.Application;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
public class HtmlInSwingExample extends JFrame {
public HtmlInSwingExample() {
setTitle("HTML5 in Swing");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JFXPanel jfxPanel = new JFXPanel();
add(jfxPanel);
SwingUtilities.invokeLater(() -> {
WebView webView = new WebView();
webView.getEngine().loadContent("<html><body><h1>Hello HTML5!</h1></body></html>");
jfxPanel.setScene(new Scene(webView));
});
}
public static void main(String[] args) {
new HtmlInSwingExample().setVisible(true);
}
}
Upvotes: 0