简体   繁体   English

新的浏览器窗口,包含Java applet生成的信息

[英]New browser window with information generated by Java applet

I have a Java applet. 我有一个Java小程序。

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[][] ). 在用户将数据输入Java applet控件后,他按下Java applet中的按钮,Java applet执行一些计算,结果是一个表(为简单表,可以认为是String[][] )。

I want to show new browser window and output this table in it as html <table> . 我想显示新的浏览器窗口并将其输出为html <table> How can this be done? 如何才能做到这一点? Preferred way is to use javascript if needed, but not involving some server side handling. 首选方法是在需要时使用javascript,但不涉及某些服务器端处理。

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. 好吧,JavaScript必须用于打开窗口并填充HTML - 这是applet根本无法访问的内容。

Luckily, JavaScript can easily call any public methods your Java applet provides. 幸运的是,JavaScript可以轻松调用Java applet提供的任何公共方法。 Unfortunately, Java cannot return a String[][] to JavaScript, but it CAN return a String. 不幸的是,Java无法将String [] []返回给JavaScript,但它可以返回一个String。

So what you can do is have your Java applet return a JSON serialization of your String[][] array. 所以你可以做的是让你的Java applet返回String [] []数组的JSON序列化。 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 ) 然后,JavaScript可以使用JSON.parse函数将该JSON转换为JavaScript数组(在大多数现代浏览器中都可用,但json.org上也提供了

So here's an example of what I am talking about (that works with at least Java 5 and Chrome): 所以这里是我所谈论的一个例子(至少与Java 5和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 JavaScript代码

<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. 首选方法是在需要时使用javascript,但不涉及某些服务器端处理。

If you really must not have any server side interaction, it'll have to be a jQuery hide/show situation. 如果你真的不能有任何服务器端的交互,它必须是一个jQuery隐藏/显示情况。

If you can bear some server side work, do it with an applet collaborating with servlet. 如果您可以承担一些服务器端工作,请使用与servlet协作的applet。 The applet won't do the calculation; 小程序不会进行计算; the servlet will. servlet会。 After it's complete, the servlet adds the result to the output page and redirects the output stream to it. 完成后,servlet将结果添加到输出页面并将输出流重定向到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM