Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display HTML5 in swing

I have seen many questions here about displaying HTML content in a swing application. But none of the libraries support html5(I cant afford JxBrowser).

I have some locally stored html5 webpages and i want to show them in my swing application. One idea is embedding firefox or chrome into my java application and removing the controls.

How can I display html5 content in my swing application?

like image 750
danrah Avatar asked Oct 02 '12 20:10

danrah


People also ask

How to use HTML in Swing?

To specify that a component's text has HTML formatting, just put the <html> tag at the beginning of the text, then use any valid HTML in the remainder. Here is an example of using HTML in a button's text: button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>"); Here is the resulting button.

Is swing good for GUI?

Swing in Java is a lightweight GUI toolkit which has a wide variety of widgets for building optimized window based applications. It is a part of the JFC( Java Foundation Classes). It is build on top of the AWT API and entirely written in java. It is platform independent unlike AWT and has lightweight components.

How to embed HTML code in Java?

Nowadays, the best way to add Java to HTML is through the so-called JavaServer Pages (JSPs). A JSP page is a file with the extension . jsp. It is an HTML markup containing several JSP tags.


3 Answers

You should look into new JavaFX browser that fully supports HTML5 and written in pure Java. You can easily integrate it into any Swing application - see the official description: http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm

But i guess it might force you to use JDK7+ to fully support various OS.

like image 176
Mikle Garin Avatar answered Sep 30 '22 18:09

Mikle Garin


Sorry to necro this old thread. But, this is still a very relevant question today, so I'd thought I'd chime in with extra information and options.

Agree with the comments above about WebKit. It's built into Java, it's fast, and it's free, and it is so easy to use. Oracle's tutorials / examples on this are some of the best and clearest I've seen on their web site (you will be up and running with a basic Java web page display app very quickly): https://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

There is really only 1 restriction / limitation with WebKit: it is JavaFX, you cannot embed it directly into Swing or AWT controls. However, you can embed the JavaFX scene in a Swing JPanel (or other container) so your Swing App can show the HTML5.

And, with Oracle now "decoupling JavaFX" (and therefore WebKit) from the main JDK / JRE, this might add extra steps to people wanting to use your Java App. e.g. they might need to download and install JavaFX for their platform separately (it it exists).

Ref: Java SE 11: The Great Removal, https://www.reddit.com/r/programming/comments/9nakq3/java_se_11_the_great_removal/

Or you could use something like GraalVM (or some other packager) to build a stand alone application and distribute that.

Ref: Ten Things You Can Do With GraalVM - Oleg Šelajev, https://www.youtube.com/watch?v=tEaEAq0L9Pk

Anyway, hope this helps someone. :)

like image 42
Wheels Of Progress Avatar answered Sep 30 '22 18:09

Wheels Of Progress


WebKit is integrated in Java 8, so you don't need to jump through hoops. It is continuously update. It is available as the JavaFX WebView control. Java also comes with the Nashorn javascript engine that can call Java from JavaScript. Overall performance is excellent.

like image 33
Aleksandr Dubinsky Avatar answered Sep 30 '22 16:09

Aleksandr Dubinsky