?
Solved

Java & JavaScript Applet error

Posted on 2004-09-27
13
Medium Priority
?
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

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