views:

189

answers:

2

I have a Java applet.

After user inputs data into the Java applet controls he presses button in Java applet and Java applet performs some calculating and result is a table (for simplicity table can be considered as String[][]).

I want to show new browser window and output this table in it as html <table>. How can this be done? Preferred way is to use javascript if needed, but not involving some server side handling.

+1  A: 

Preferred way is to use javascript if needed, but not involving some server side handling.

If you really must not have any server side interaction, it'll have to be a jQuery hide/show situation.

If you can bear some server side work, do it with an applet collaborating with servlet. The applet won't do the calculation; the servlet will. After it's complete, the servlet adds the result to the output page and redirects the output stream to it.

duffymo
+2  A: 

Well, JavaScript will have to be used to open the window and populate the HTML - this is something the applet simply has no access to.

Luckily, JavaScript can easily call any public methods your Java applet provides. Unfortunately, Java cannot return a String[][] to JavaScript, but it CAN return a String.

So what you can do is have your Java applet return a JSON serialization of your String[][] array. JavaScript can then convert that JSON to a JavaScript array using the JSON.parse function (available on most modern browsers, but there are also libaries available at json.org)

So here's an example of what I am talking about (that works with at least Java 5 and Chrome):

The Applet Code

import java.applet.*;
public class Test extends Applet {
 String data[][];
 public void init() {
  data = new String[5][2];
  data[0] = new String[] { "Peter", "Griffin"};
  data[1] = new String[] { "Glen", "Quagmire"};
  data[2] = new String[] { "Joe", "Something"};
  data[3] = new String[] { "Cleveland", "Brown"};
  data[4] = new String[] { "Buzz", "Killington"};
 }

 public String getData() {
  return toJSON(data);
 }

 /* Quick and dirty, you may want to look
 * online for a 'complete' converter
 *
 * This returns [["Peter", "Griffin"], ["Glen", "Quagmire"], ... etc
 */
 protected String toJSON(String data[][]) {
  int x, y;
  String s = "[";
  for (y = 0;y < data.length;y += 1) {
   s += "[";
   for (x = 0;x < data[y].length;x += 1) {
    s += "\""+data[y][x].replaceAll("\"","\\\"")+"\"";
    if (x < data[y].length-1) {
     s += ", ";
    }
   }
   s += "]";
   if (y < data.length-1) {
    s += ", ";
   }
  }
  s += "]";

  return s;
 }
}

The JavaScript Code

<html>
<body>
<p><applet id="applet" code="Test"></applet></p>
<input id="button" type="button" value="Click me"/>

<script type="text/javascript">
(function () {
 var button = document.getElementById("button"), applet = document.getElementById("applet");
 button.onclick = function () {
  var html = [], newWindow, data = JSON.parse(applet.getData()), j;

  html.push("<table><tr><th>First Name</th><th>Last Name</th></tr>");
  for (j = 0;j < data.length;j += 1) {
   html.push("<tr><td>"+data[j][0]+"</td><td>"+data[j][1]+"</td></tr>");
  }
  html.push("</table>");

  newWindow = window.open();
  newWindow.document.firstChild.innerHTML = html.join("");
 };
}());
</script>

Let me know if you need further clarification!

Matt
+1. Thanks a lot! I will try it later.Actually it is enough for me for now to know how to output string, not a table - I would like to understand how java and java-script should communicate. But for table it is even better.
sergdev
I see in your code the button is created in java-script. But I have button in applet (I just updated question). Is it possible to adjust the code to account this? Thanks in advance.
sergdev
That's a little trickier and I'm afraid I've never actually done that. However, I can tell you that you must use the LiveConnect API's JSObject, and this specific section in Sun's documentation will tell you how: https://jdk6.dev.java.net/plugin2/liveconnect/#JAVA_TO_JS
Matt
@Matt your last link helped me. Thanks!
sergdev