开发者

New browser window with information generated by Java applet

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 <tabl开发者_Python百科e>. How can this be done? Preferred way is to use javascript if needed, but not involving some server side handling.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜