Solved

Exchange arrays between Java applets and JavaScript functions?

Posted on 1997-05-13
5
190 Views
Last Modified: 2013-11-23
In a html-page I want to exchange arrays beetween JavaScript functions and Java applet functions.

I´ve tried the following code, but it returns with the script error
"No Java method matching arguments."

Here are my sample files:

JavaParameterTest.java

import java.applet.*;
public class JavaParameterTest extends Applet
{
      public JavaParameterTest()
      {
      }
      public String Test(String Ar[], int i) {
          return Ar[i];
      }
}

JavaParameterTest.html:

<html>
<head>
<title>JavaParameterTest</title>
<script>
testarray = new Array("Peter","Paul","Mary");
function test() {
  alert(document.JavaParameterTest.Test(testarray(),1));
}
</script>
</head>
<body onload="test();">
<applet
    code=JavaParameterTest.class
    name=JavaParameterTest
 </applet>
</body>
</html>

I there any chance to modify this code, so does it works on IE 3.0 and NS 3.0?

I would appreciate any help.
0
Comment
Question by:StephanM
  • 3
5 Comments
 

Expert Comment

by:NickRS051397
ID: 1220391
In the JavaScript you're trying to call a function testarray().
To pass an array use testarray[].
0
 

Author Comment

by:StephanM
ID: 1220392
Your proposal is not the right solution, because if I change
the argument for my applet from testarray() to testarray[],
it produces a syntax error in Netscape. Sorry.
0
 

Author Comment

by:StephanM
ID: 1220393
Adjusted points to 120
0
 
LVL 3

Accepted Solution

by:
garik earned 120 total points
ID: 1220394
1) JavaScript: add MAYSCRIPT tag to the applet to allow Java access to JavaScript objects; pass testarray as an object (not as function testarray()):
...
<script>
testarray = new Array("Peter","Paul","Mary");
function test() {
      alert(document.JavaParameterTest.Test(testarray,1));
}
</script>
</head>
<body onLoad='test();'>
<hr>
<applet      MAYSCRIPT
    code=JavaParameterTest.class
    name=JavaParameterTest
>
</applet>
...

2) Java: first argument to Test() is JSObject, so you have to import netscape.javascript.*; JSObject can't be casted to String[], but you can use JSObject.getSlot(index) to access individual members of the array:

import java.applet.*;
import netscape.javascript.*;
...
      public String Test(JSObject Ar, int i)
      {
            return (String)Ar.getSlot(i);
      }
...

3. MS IE: Not a chance. LiveConnect technology used here to communicate between Java and JavaScript is created by Netscape, implemented by Netscape, and works only on Netscape. Sorry.
For MS, you have to use MS staff, like Java-COM communications etc.
Wouldn't be nice to get rid of one of these big guys (I don't care which one) so that we could concentrate on contents and not on technical incompatibilities? Just a thought.

0
 

Author Comment

by:StephanM
ID: 1220395
Thanks for your response. I´ve checked it and it´s true -
there is in fact no chance to exchange arrays in the ie.

Concentrating on content and not on incompatibilities
would be nice, but I don´t think it will happen.

Over and out, StephanM.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question