?
Solved

Exchange arrays between Java applets and JavaScript functions?

Posted on 1997-05-13
5
Medium Priority
?
201 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 240 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

807 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