Java小程序和网页上的文本输入控件之间的桥梁 [英] Bridge between the Java applet and the text input controls on the web page

查看:21
本文介绍了Java小程序和网页上的文本输入控件之间的桥梁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Java 小程序,该小程序有助于仅使用鼠标进行编写.就我而言,我正在尝试将其合并到我的网站项目中,如下所示:

I have been working with a Java applet which is an applet that helps to write using only a mouse. For my case, I am trying to incorporate this into my webiste project as follows:

当用户点击页面上的任何输入元素(文本框/文本区域)时,这个 JAVA 小程序会加载到网页本身上.在下面看到的 JAVA 小程序的屏幕截图中,用户指向一个字母 to 并且相应的文本被写入小程序的文本框中.

When the user clicks on any input element (textbox/textarea) on the page, this JAVA applet loads on the webpage itself. In the screenshot of the JAVA applet seen below, the user points to an alphabet to and the corresponding text gets written in the text box of the applet.

现在我要做的是从applet 的TextBox 中获取这个文本到网页上的输入元素.我知道这需要 Java 和 JavaScript 之间的交互,但不是专业人士,我真的没有把握.这是我编写的 Java 小程序和代码.

Now what I am trying to do is to get this text from the TextBox of the applet to the input element on the webpage. I know that this needs an interaction between the Java and JavaScript, but not being a pro, I really do not have the catch. Here's the Java applet and the code I have written.

Java 小程序和 jQuery 代码 (298kB):http://bit.ly/jItN9m

Java applet and jQuery code (298kB): http://bit.ly/jItN9m

请帮忙扩展此代码.非常感谢!

Please could somebdoy help for extending this code. Thanks a lot!

更新

我在某处搜索并找到了这个 -> 要获取 Java 文本框内的文本,Applet 中用于检索文本的 getter 方法:

I searched somewhere and found this -> To get the text inside of Java text box, a getter method in the Applet to retrieve the text:

public class MyApplet extends JApplet {
  // ...
  public String getTextBoxText() { return myTextBox.getText(); }
}

在JQuery代码中,我认为需要添加以下几行:

In the JQuery code, the following lines are to be added I think:

var textBoxText = $("#applet-id")[0].getTextBoxText();
//Now do something with the text

关于小程序的代码,我在这里看到了一个GNOME git页面.getText 调用已经存在——查看此文件的底部:http://git.gnome.org/browse/dasher/tree/java/dasher/applet/JDasherApplet.java

For the code of the applet, I saw a GNOME git page here. The getText call already exists -- look at the bottom of this file: http://git.gnome.org/browse/dasher/tree/java/dasher/applet/JDasherApplet.java

我需要调用getCurrentEditBoxText",但是什么时候应该调用这个方法getCurrentEditBoxText"?就我而言,当用户单击新的输入控件等时,我可能不得不这样做.

I'd need to call 'getCurrentEditBoxText' but when should this method 'getCurrentEditBoxText' be called? In my case, I would probably have to do it when the user clicks in a new input control etc.

推荐答案

您可以在您的 Applet 和页面上的任何 javascript 方法之间进行完全通信.Kyle 有一篇很好的帖子演示了 Javascript 如何调用小程序并请求文本值.但是,我假设您希望每次单击鼠标时 HTML 文本字段都会更新,这意味着小程序需要与页面进行通信.我会把你的 javascript 修改成这样:

You can have full communication between your Applet and any javascript method on the page. Kyle has a good post demonstrating how the Javascript can call the applet and request the text value. However, I presume you want the HTML Textfield to update with each mouse click, meaning the applet needs to communicate with the page. I would modify your javascript to something like this:

var activeTextArea = null;

$('textarea, input').click(function() {
    $(this).dasher();
    activeTextArea = this;
}); 

function updateText(text) {
     // Careful: I think textarea and input have different 
     // methods for setting the value. Check the 
     // jQuery documentation
     $(activeTextArea).val(text); 
}

假设你有小程序的源代码,你可以让它与上面的javascript函数通信.添加此导入:

Assuming you have the source for the applet, you can have it communicate with the above javascript function. Add this import:

import netscape.javascript.JSObject;

然后,在鼠标点击的任何 onClick 处理程序中,添加:

And then, in whatever onClick handler you have for the mouse clicks, add:

// After the Applet Text has been updated
JSObject win = null;
try {
    win = (JSObject) JSObject.getWindow(Applet.this);
    win.call("updateText", new Object[] { textBox.getText() });
} catch (Exception ex) {
    // oops
}

每次调用该代码块时都会更新文本.如果您无法访问小程序源,事情就会变得棘手.您需要设置某种 javascript 超时方式,不断从小程序中读取值,但这假设小程序具有返回文本框值的方法.

That will update the text each time that chunk of code is called. If you do NOT have access to the applet source, things get trickier. You'd need to set some manner of javascript timeout that constantly reads the value from the applet, but this assumes the applet has such a method that returns the value of the textbox.

另见:http://java.sun.com/products/plugin/1.3/docs/jsobject.html

更新 修改小程序是您最好的选择,因为这是触发任何事件的地方.例如,如果您希望 HTML 文本字段在每次单击时更改,则单击发生在小程序中,小程序需要修改以触发更新,如上所述.在不修改小程序的情况下,我看到两个选项.选项 #1 使用计时器:

Update Modifying the applet is your best shot since that is where any event would be triggered. For example, if you want the HTML TextField to change on every click, the click happens in the applet which would need to be modified to trigger the update, as described above. Without modifying the applet, I see two options. Option #1 uses a timer:

var timer;
var activeTextArea;

$('textarea, input').click(function() {
    $(this).dasher();
    activeTextArea = this;
    updateText();
} 

function updateText() {
    // Same warnings about textarea vs. input
    $(activeTextArea).val($('#appletId')[0].getCurrentEditBoxText());
    timer = setTimeout("updateText()", 50);
}

function stopUpdating() {
    clearTimeout(timer);
}

这与上面的代码类似,只是点击文本区域会触发循环函数updateText(),它将每 50 毫秒将 HTML 文本字段的值设置为 Applet 文本字段的值.这可能会在点击和更新之间引入一个小的延迟,但它会很小.您可以增加计时器频率,但这会增加性能消耗.我没有看到您隐藏"了小程序的位置,但是同一个函数应该调用 stopUpdating 以便我们不再尝试联系隐藏的小程序.

This is similar to the code above except clicking on a text area triggers the looping function updateText() which will set the value of the HTML text field to the value of the Applet text field every 50ms. This will potentially introduce a minor delay between click and update, but it'll be small. You can increase the timer frequency, but that will add a performance drain. I don't see where you've 'hidden' the applet, but that same function should call stopUpdating so that we are no longer trying to contact a hidden applet.

选项#2(未编码)

我将尝试捕获 Applet 中的点击,因为它在 HTML Dom 中冒泡.然后,您可以跳过计时器并在 Applet 容器上放置一个 click() 行为来执行相同的更新.不过,我不确定此类事件是否会冒泡,因此不确定这是否可行.即使是这样,我也不确定它跨浏览器的兼容性如何.

I would be to try and capture the click in the Applet as it bubbles through the HTML Dom. Then, you could skip the timer and put a click() behavior on the Applet container to do the same update. I'm not sure if such events bubble, though, so not sure if this would work. Even if it did, I'm not sure how compatible it would be across browsers.

选项#3

第三个选项是不要在每次点击时更新 HTML 文本字段.这将简单地结合 Kyle 和我上面的帖子,以在您完成"小程序时设置文本字段的值.

Third option is to not update the HTML text field on every click. This would simply be a combination of Kyle's and my posts above to set the value of the text field whenever you 'finish' with the applet.

这篇关于Java小程序和网页上的文本输入控件之间的桥梁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆