Solved

Java & JavaScript Applet error

Posted on 2004-09-27
13
377 Views
Last Modified: 2013-12-16
I was trying to create an applet and I have some problems.

Here is the code:

// File: sleepclass.java
public class sleepclass
{
  public void sleep(float timeInMillis) throws InterruptedException
  {
    Thread.sleep((long)timeInMillis);
  }
}

// File: test.htm
<html>
<head></head>
<body>
<APPLET CODE="sleepclass.class" WIDTH=1 HEIGHT=1></applet>
<script language="javascript">
   window.status  = "abc";
   window.document.applets[0].sleep(2000);
   window.status += "def";
</script>
</body>
</html>


Then, I compile sleepclass.java:

c:\> javac sleepclass.java


When I launch test.htm, I get a JavaScript error in line 'window.document.applets[0].sleep(2000);'


Some idea?
0
Comment
Question by:Eligio
  • 9
  • 4
13 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12161000
public class sleepclass extends java.applet.Applet

0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161013
you also probably want to fire the javascript when a button is pressed, etc...  As the applet will probably load into the browser AFTER the javascript has run...
0
 

Author Comment

by:Eligio
ID: 12161111
Thanks TimYates, but the problem continues. Now I have:

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public void sleep(float timeInMillis) throws InterruptedException
  {
    Thread.sleep( (long) timeInMillis );
  }
}

// File: test.htm
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    window.document.applets[0].sleep(2000);
    window.status += "def";
  };
</script>
</head>
<body >
<APPLET CODE="sleepclass.class" WIDTH=100 HEIGHT=10></applet>
<input type="button" onclick="fncInit();">
</body>
</html>


But error is the same. I let the applet to finish loading and then I press the button, but I always get a JS error. The error description (traslated from spanish) is: Object doesn't accept this property or method.

Thanks
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 35

Expert Comment

by:TimYates
ID: 12161178
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    document.getElementById( 'VA' ).sleep( 2000 ) ;
    window.status += "def";
  };
</script>
</head>
<body >
  <applet id="VA" width="1" height="1" code="sleepclass" MAYSCRIPT>
  </applet>
  <input type="button" onclick="fncInit();">
</body>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161284
If that doesn't work, try this for your applet (get rid of the "throws")

public class sleepclass extends java.applet.Applet
{
  public void sleep( int timeInMillis )
  {
    try { Thread.sleep( timeInMillis ); } catch( InterruptedException ex ) {}
  }
}

also, why were you passing "float" to this method?
0
 

Author Comment

by:Eligio
ID: 12161539
TimYates, same error.

I used float beacause I read that javascript numeric arguments are always catched by the applet as floats, and then the call must be with a long ones.

Don't know what happends, beacause if I try to write a more simple applet, it always throws the same error.

public class A extends java.applet.Applet
{
  public void B( ) { //none }
}

When I do:
document.applets[0].B()
or
document.applets("my_applet_name").B()
or
document.my_applet_name.B()
or
document.getElementById(my_applet_id).B()

always get an error.

If I do: alert(document.applets[0].id), VA is printed, so I supose the applet is well referenced.


Any idea?


Thanks
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161591
Try...

What browser are you using?

Tim

-----------------

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public String woo()
  {
    return "woo!" ;
  }
}

// File: test.htm
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    alert( window.getElementById( "VA" ).woo() ) ;
    window.status += "def";
  };
</script>
</head>
<body >
<APPLET id="VA" CODE="sleepclass" WIDTH=100 HEIGHT=10 MAYSCRIPT></applet>
<input type="button" onclick="fncInit();">
</body>
</html>
0
 

Author Comment

by:Eligio
ID: 12161624
One more thing...

I have edited sleepclass.java, and add this code:

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public void init()
  {
    System.out.println("We have been initialized");
  }
      
  public void sleep( int timeInMillis )
  {
    try { Thread.sleep( timeInMillis ); } catch( InterruptedException ex ) {}
  }
}

If I run: appletviewer test.htm, I can see "We have been initialized" on OS symbol.

But when I try to call the function ... crash!

Maybe this help you.


Thanks.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161657
You do have the java pluing installed don't you?

Again, what browser are you using?  On what OS?

can you open the java console?  does that print "We have been initialised" when you open the webpage in your browser?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161682
And...what JDK are you using to compile the applet?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 12161690
And what version of the plugin do you have installed in your browser?
0
 

Author Comment

by:Eligio
ID: 12161758
Thanks TimYates.

After testing the the applet in other computer it worked. Then I reinstalled java and now it works fine too.



0
 
LVL 35

Expert Comment

by:TimYates
ID: 12162262
Yay!! :-)

Good luck with it all! :-)

Tim
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 36
Way to decrease size of apk file 9 69
tomcat administrtor 12 47
How to determine if a string is a valid SHA value 7 34
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…
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

777 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