简体   繁体   English

Java applet与网页上的文本输入控件之间的桥接

[英]Bridge between the Java applet and the text input controls on the web page

I have been working with a Java applet which is an applet that helps to write using only a mouse. 我一直在使用Java applet,这是一个小程序,可以帮助只使用鼠标编写。 For my case, I am trying to incorporate this into my webiste project as follows: 对于我的情况,我试图将其合并到我的网站项目中,如下所示:

When the user clicks on any input element (textbox/textarea) on the page, this JAVA applet loads on the webpage itself. 当用户单击页面上的任何输入元素(textbox / textarea)时,此JAVA小程序将加载到网页本身。 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. 在下面看到的JAVA applet的屏幕截图中,用户指向一个字母表,相应的文本写在applet的文本框中。

在此输入图像描述

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. 现在我要做的是将这个文本从applet的TextBox传递到网页上的input元素。 I know that this needs an interaction between the Java and JavaScript, but not being a pro, I really do not have the catch. 我知道这需要Java和JavaScript之间的交互,但不是专业人士,我真的没有抓住。 Here's the Java applet and the code I have written. 这是Java applet和我编写的代码。

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

Please could somebdoy help for extending this code. 请大家帮忙扩展这段代码。 Thanks a lot! 非常感谢!

Update 更新

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: 我在某个地方搜索并发现了这个 - >要获取Java文本框内的文本,在Applet中使用getter方法来检索文本:

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

In the JQuery code, the following lines are to be added I think: 在JQuery代码中,我想要添加以下行:

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

For the code of the applet, I saw a GNOME git page here. 对于applet的代码,我在这里看到了一个GNOME git页面。 The getText call already exists -- look at the bottom of this file: http://git.gnome.org/browse/dasher/tree/java/dasher/applet/JDasherApplet.java getText调用已存在 - 请查看此文件的底部: http//git.gnome.org/browse/dasher/tree/java/dasher/applet/JDasherApplet.java

I'd need to call 'getCurrentEditBoxText' but when should this method 'getCurrentEditBoxText' be called? 我需要调用'getCurrentEditBoxText',但什么时候应该调用这个方法'getCurrentEditBoxText'? In my case, I would probably have to do it when the user clicks in a new input control etc. 就我而言,当用户点击新的输入控件等时,我可能不得不这样做。

You can have full communication between your Applet and any javascript method on the page. 您可以在Applet和页面上的任何javascript方法之间进行完全通信。 Kyle has a good post demonstrating how the Javascript can call the applet and request the text value. Kyle有一个很好的帖子,演示了Javascript如何调用applet并请求文本值。 However, I presume you want the HTML Textfield to update with each mouse click, meaning the applet needs to communicate with the page. 但是,我认为您希望每次单击鼠标都能更新HTML Textfield,这意味着applet需要与页面进行通信。 I would modify your javascript to something like this: 我会修改你的javascript到这样的:

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); 
}

Assuming you have the source for the applet, you can have it communicate with the above javascript function. 假设您拥有applet的源代码,您可以让它与上面的javascript函数进行通信。 Add this import: 添加此导入:

import netscape.javascript.JSObject;

And then, in whatever onClick handler you have for the mouse clicks, add: 然后,在用于鼠标单击的任何onClick处理程序中,添加:

// 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
}

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. 如果您无权访问applet源,事情会变得棘手。 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. 您需要设置某种javascript超时方式,不断从applet中读取值,但这假设applet具有返回文本框值的方法。

See Also: http://java.sun.com/products/plugin/1.3/docs/jsobject.html 另请参阅: http//java.sun.com/products/plugin/1.3/docs/jsobject.html

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. 例如,如果您希望HTML TextField在每次单击时都发生更改,则会在applet中进行单击,需要修改该单击以触发更新,如上所述。 Without modifying the applet, I see two options. 在不修改applet的情况下,我看到了两个选项。 Option #1 uses a timer: 选项#1使用计时器:

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);
}

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. 这与上面的代码类似,只是单击文本区域会触发循环函数updateText() ,它将每隔50ms将HTML文本字段的值设置为Applet文本字段的值。 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. 我没有看到你'隐藏'applet的位置,但是同样的函数应该调用stopUpdating以便我们不再尝试联系隐藏的applet。

Option #2 (not coded) 选项#2(未编码)

I would be to try and capture the click in the Applet as it bubbles through the HTML Dom. 我将尝试捕获Applet中的点击,因为它在HTML Dom中冒泡。 Then, you could skip the timer and put a click() behavior on the Applet container to do the same update. 然后,您可以跳过计时器并在Applet容器上放置click()行为以执行相同的更新。 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. 即使它确实如此,我也不确定它与浏览器的兼容性如何。

Option #3 选项#3

Third option is to not update the HTML text field on every click. 第三个选项是不在每次点击时更新HTML文本字段。 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. 这只是Kyle和我上面帖子的组合,可以在你完成applet时设置文本字段的值。

Here's a possible solution. 这是一个可能的解决方案。 To get the text inside of your Java text box, write a getter method in the Applet to retrieve the text: 要在Java文本框中获取文本,请在Applet中编写getter方法以检索文本:

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

In your JQuery code, add the following lines: 在您的JQuery代码中,添加以下行:

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

I found most of what I posted above here . 我发现,大多数的什么我上面张贴在这里 Hope this helps. 希望这可以帮助。

This page explains how to manipulate DOM from a Java applet. 本页介绍了如何从Java applet操作DOM。 To find the input element, simply call the document.getElementById(id) function with id of an id attribute of the text input box. 要查找input元素,只需调用document.getElementById(id)函数,其id为文本输入框的id属性。

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

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