2

I was wondering how I could embed a browser like view in a Java client application, at the same time that I can interact with it by means of JavaScript. The problem that triggered this question is the following:

The interface of my application consists of a (Google) map and some svg stuff. This is easy to do in a browser. However, I also need access to some special Java libraries that can process some information from the map (e.g., certain coordinates in the map) and that answers values that should influence the browser view (e.g., a path should be drawn over the map).

My first idea was to implement the Java side behaviour as a REST web service, so from JavaScript I will invoke this webservice sending relevant information about the map and using the answer to update the map. Nevertheless, for my current needs (this is only a prototype) using webservices is a bit too much of infrastructure.

Is there a way I could just:

  • embed a browser like view in my Java application.
  • Interact from the Java side with this view by means of JavaScript functions implemented in the web page displayed in the browser view (such as these functions will influence the rendering of the page in the browser like view) ?.

I found other questions related to how to embed a browser in a swing application (e.g., Embedding web browser window in Java) and JDIC seems to be able to do this. Although some people report it is difficult to make it work in OSX (the OS I use) and do not mention if it is possible to interact with the browser by means of JavaScript.

It seems to me that in Android it is possible to make JavaScript calls from the Java (Android) side, so probably this is also possible in plain Java.

Thanks for any pointer !

Community
  • 1
  • 1
Sergio
  • 8,532
  • 11
  • 52
  • 94

2 Answers2

2

If you can use swt, take a look at SWT Browser widget

For javascript you can use http://www.mozilla.org/rhino/

fahim ayat
  • 312
  • 5
  • 10
  • Hi @Fahim, so with Rhino I can interact from my Java program with the JavaScript in the SWT Browser widget ?. Is the inverse also possible ? , for example, to invoke a Java method from the JavaScript side ? – Sergio May 20 '12 at 12:51
  • 1
    1. SWT Browser widget and Rhino are two different concept. in swt you have an embeded browser which can render html content to show to user, with rhino you can execute javascript code, you can use these two components to get what you want, and yes, in javascript you can interact with java object , see http://www.mozilla.org/rhino/tutorial.html#useJava , and sorry that it took me a little while to answer, – fahim ayat May 20 '12 at 14:09
2

With Java FX 2 you can. You get a webkit webview there. Can interact with it back and forth with java<->javascript.

For an example embedding google maps see: http://java-buddy.blogspot.se/2012/03/embed-google-maps-in-javafx-webview.html

You can embed java fx in swing with JFXPanel if you don't want to go with 100% JavaFX yet.

Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
  • Hi @Pulsar, could you tell me which would be the advantage of using Java FX 2 over using a SWT Browser widget as it is mentioned in another answer ? – Sergio May 20 '12 at 12:55
  • Java FX 2 is the new and improved (and offical) GUI framework for Java and will be part of JDK 8 (already get it as part of the JDK 7 download now). It integrates very nicely with swing too. It is pure java (which java fx 1.x was not as it used java fx script not java). Having said that SWT is proven and stable and JavaFX (now at version 2.1) is not as wide spread yet and not available as a final release on linux yet. JavaFX is the future of rich client java gui though and using modern principles such as a scene graphs and animations etc. – Mattias Isegran Bergander May 20 '12 at 13:04
  • Hello @Pulsar!, after reading your answer I spent some time browsing internet about Java FX 2. It seems to me that many people agree that this technology is going to be (eventually) replaced by HTML 5. Do you know if there is a way to do what I need using HTML 5 instead of Java FX 2 ?. – Sergio May 20 '12 at 13:24
  • Woha, that's a large topic... But you are sort of doing that already, as you want an embedded browser. JavaFX 2 can embedd html5 inside as a webview, providing you the best of both worlds. Pure HTML 5 would mean no client application, just a pure web app. – Mattias Isegran Bergander May 20 '12 at 13:32