JxBrowser 6.1 JavaScript Java Bridge API is not working - java

I think that there is a problem with the JavaScript Java Bridge API in JxBrowser 6.1 . I have tried a very simple code to call a method of a java class in Javascript. Here are the codes. In java, java is set as a property on javascript window object to an instance of Events class and then the html is loaded. In html, I simply call Close method of Events class. But when I click the Close button, java Close function doesn't get called and there is a message in console from JxBrowser saying :
Uncaught TypeError: Cannot read property 'Close' of undefined
which means that java property for window object is not defined.
Main.java:
public class Main extends Application {
private Browser browser;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Platform.setImplicitExit(false);
browser = new Browser();
JSValue window = browser.executeJavaScriptAndReturnValue("window");
window.asObject().setProperty("java", new Events());
BrowserView browserView = new BrowserView(browser);
StackPane pane = new StackPane();
pane.getChildren().add(browserView);
Scene scene = new Scene(pane, 330, 470);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setScene(scene);
primaryStage.show();
browser.loadURL(Main.class.getResource("templates/simple.html").toExternalForm());
}
}
class Events {
public void Close() {
System.out.println("close button clicked");
}
}
simple.html:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<button id="Close">Close</button>
<script>
document.getElementById('Close').onclick = function () {
window.java.Close();
}
</script>
</body>
</html>
Here is the article i've used to do this:
https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript
Please correct me if i'm wrong.
Thanks in advance.

Please make sure that you load required web page before you access its JavaScript and register Java objects. For example:
browser.addLoadListener(new LoadAdapter() {
#Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) {
Browser browser = event.getBrowser();
JSValue value = browser.executeJavaScriptAndReturnValue("window");
value.asObject().setProperty("Account", new Account());
}
}
});
browser.loadURL("form.html");

Related

Retrieve data into Java Application from an HTML file that was launched

So I have been searching for a way to get current location in java (not using any Android APIs) and something that is quite accurate. What I have so far is running an HTML file inside of Java and what I want to do is retrieve the GPS coordinates from a google API (html file).
What I have so far:
HtmlRun.java
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class HtmlRun {
public static void main(String[] args) throws IOException {
File htmlFile = new File("findLocation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
}
}
findLocation.html
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBTQzs7iZUtr7v-VbvAWhAql5LiQ9zZrFE&callback=initMap">
</script>
</body>
</html>
When I run this, it opens up my default app for html files and everything is sorted... but how could I retrieve the information I need? (GPS coordinates)
It occurs over here:
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
But I don't know how to retrieve that data back into my Java application.
This here:
public static void main(String[] args) throws IOException {
File htmlFile = new File("findLocation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
}
is just opening the html doc in a web browser, after that you have no control AT ALL about what is happening with the web content, the user can even click all the buttons/ pois in the map and you will never get access to that information.
What you need is a webserver that SERVES the content of that app, you can implement REST/full services or even WebSockets to exchange the data between Browser and server

calling javascript function from within java applet - Firefox vs. IE

HTML code:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-15" />
<script type="text/javascript">
var foo;
function test(s)
{
foo = s;
alert(foo);
}
window.onload = function ()
{
foo = 'init';
document.getElementById('foo').doit();
alert(foo);
}
</script>
<body>
<applet id="foo" code="TestApplet.class"
archive="TestApplet.jar" width="0" height="0"></applet>
</body></html>
Java applet code:
public void doit ()
{
try {
getAppletContext().showDocument(
new URL("javascript:test(\"foobar\")"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
When I call this HTML page :
In IE (8) I get: First popup ("foobar"), second popup ("foobar").
In Firefox (19.2, 30.0) I get: First popup ("foobar"), second popup ("init").
Can anybody explain why? Obviously, foo is written by the applet only temporarily.
Many thanks in advance.

javafx webview load local css files

I have a WebView, which loads a local html file that I have saved within my project. I use the following to load the file:
InputStream is = Browser.class.getResourceAsStream(location);
String str = "";
int i;
while((i = is.read()) != -1){
str += (char)i;
}
str = str.replace("{placeholder_1}", value1);
str = str.replace("{placeholder_2}", value2);
webEngine.loadContent(str);
In the HTML I have a link to a css file. The HTML file and the css file are in the same directory, but the css file isn't loading when the page loads in the WebView. Here is how I am calling the css file from the HTML:
<link rel="stylesheet" href="main.css" />
Why is the file not loading? According to others, this is how they are doing it and it is working. Why is it not working for me, what am I doing wrong?
Here is the directory layout:
Putting the css file in the same directory as the html file, will work if
webView.getEngine().load(location);
is used. However it will not work for loadContent(). You need to explicitly define the css file location as:
(pseudo-code)
str = str.replace("href='main.css'",
"href='" + getClass().getResource("main.css") + "'");
The following thing is working for me
Project Structure
Application
|
src
|
package
|
WebViewLoadLocalFile.java
test.html
test.css
WebViewLoadLocalFile
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewLoadLocalFile extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane borderPane = new BorderPane();
WebView webView = new WebView();
String url = getClass().getResource("test.html").toExternalForm();
webView.getEngine().load(url);
borderPane.setCenter(webView);
final Scene scene = new Scene(borderPane);
stage.setScene(scene);
stage.setHeight(300);
stage.setWidth(250);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
test.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
<link rel="stylesheet" href="test.css" />
</head>
<body>
Test HTML
</body>
</html>
test.css
body
{
background-color:#d0e4fe;
}

Creating and running a Java applet

I thought I would try my hand at applets - I made an applet using Eclipse. It runs fine using the Run As -> Java Applet.
I read a bit about running it outside Eclipse, so I did the following:
Made a folder.
Created New -> Java Project [applet_test].
Inside the project, I created New -> Other -> Visual Swing Class -> Applet [Number1] - that created Number1.class.
Added code and ran it as a Java applet - it ran fine.
Exported the project as a JAR file (not a runnable JAR file).
Wrote HTML using TextEdit (Mac's version of Windows' Notepad). The HTML follows, below...
I put the JAR file, HTML and .class file in the folder.
In Terminal (Mac's version of Windows command prompt window), I ran Appletviewer applet_testX2.html (that's the name of my HTML).
I could see a brief flash of the application name at the top of the screen (as would any other running application).
However, the application (which should display a Jpanel with a label and a button) did NOT appear. I also tried running it from Firefox and Safari. Only the HTML code appeared.
So, what am I doing wrong? And, more importantly, how do I do it correctly?
Code follows without imports statements:
<html>
<body>
<applet code="Number1.class" archive="applet_test.jar"
width=300
height=300>
</applet>
</body>
</html>
The Java code:
public class Number1 extends JApplet {
public Number1() {
}
private static final long serialVersionUID = 1L;
#Override
public void init() {
try {
EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
initComponents();
}
});
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
setSize(320, 240);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
JLabel lblAppletTest = new JLabel("Applet test 1");
panel.add(lblAppletTest);
JButton btnPushIt = new JButton("Push it");
panel.add(btnPushIt);
}
}
Firefox source view:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Author" content="BT">
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1038.35">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica; min-height: 19.0px}
</style>
</head>
<body>
<p class="p1"><html></p>
<p class="p1"><span class="Apple-converted-space"> </span><body></p>
<p class="p1"><span class="Apple-converted-space"> </span><applet code="Number1.class" archive="applet_test.jar"</p>
<p class="p2"><br></p>
<p class="p1"><span class="Apple-converted-space"> </span>width=300</p>
<p class="p1"><span class="Apple-converted-space"> </span>height=300></p>
<p class="p1"></applet></p>
<p class="p1"></body></p>
<p class="p1"></html></p>
</body>
</html>
My guess is that here:
<applet code="Number1.class" archive="applet_test.jar"
you're not taking packages into consideration. For instance, if the package is myPackage.vol3 then the line should read
<applet code="myPackage.vol3.Number1.class" archive="applet_test.jar"
But if this doesn't help, you'll want to extract any error messages that the browser gives you and edit your original post to show us what they are.
Using Appletviewer
------------------
Write code of Applet.
If you installed tomcat in D:
code
-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyApplet extends Applet
{
public void init()
{
System.out.println("init intilize");
GridLayout g=new GridLayout(4,6,0,0);
setLayout(g);
MyListener m=new MyListener();
for(int i=1;i<=12;i++)
{
Button b=new Button("ok"+i);
add(b);
b.addActionListener(m);
}
}//end of init
public void start()
{
System.out.println("applet started");
}//end of start
public void stop()
{
System.out.println("applet stop");
}//end of
public void paint(Graphics g)
{
g.drawString("Naveed",200,25);
g.drawOval(20,30,30,20);
System.out.println("applet paint");
}//end of start
public void destroy()
{
System.out.println("applet destroy");
}//end of start
}
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("button clicked");
}//end of actionPerformed
}
Now save this code in D:, not in sub folder.
First Compile it.
open the cmd
cd D:
type
`javac MyApplet.java -d classpath D:\Tomcat\common\lib\servlet.jar`
This will make a MyApplet.class file
Now make a html file.
<html>
<body>
<applet code="Number1.class" width=30 height=300 > </applet>
</body>
</html>
Save with the name of you want let's say app.html
run the html file now.
In the cmd window
appletviewer app.html
Output will be in front of you.

Applet Permanently Loses Focus When Leaving Browser and Coming Back

I have a web page with an applet as the only element that looks something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>...</title>
</head>
<body>
<applet style="padding:1px; border:1px solid gray" mayscript="mayscript" codebase="..." name="AppletName" code="..." archive="..." width="600" height="500" alt="Alt Text">
<param name="initial_focus" value="true"/>
Alt Text
</applet>
</body>
</html>
When the page initially loads, focus is set in the applet and I can tab through and interact with the applet just fine. However, if I leave the browser window and then come back to it, I can no longer regain focus on the applet just using the tab key.
Pressing F5 to reload the page fixes the page so that the Applet regains focus, but this solution is unacceptable.
How do I solve this problem? Thanks.
Tentative solution:
//Dean Edwards/Matthias Miller/John Resig
function init() {
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
// kill the timer
if (_timer) clearInterval(_timer);
window.onfocus = function() {
if(!document.AppletName.isActive())
document.AppletName.requestFocus();
};
}
/* for Mozilla/Opera9 */
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false);
}
/* for Internet Explorer */
/*#cc_on #*/
/*#if (#_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
init(); // call the onload handler
}
};
/*#end #*/
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
init(); // call the onload handler
}
}, 10);
}
/* for other browsers */
window.onload = init;
Note that the key part for detecting whether the applet needs focus and requesting it if so is (this only works if mayscript is enabled):
if(!document.AppletName.isActive())
document.AppletName.requestFocus();
The rest of the code is just attaching the window on focus handling after the page is loaded (using the script JQuery.ready is based off of).
Better solutions welcome.

Categories