Solved

Java & JavaScript Applet error

Posted on 2004-09-27
13
379 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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
MySQL  on Tomcat 8 71
How to execute a Python program and gather return output in Java 2 41
java example issue 5 26
Running JavaFX on the Raspberry Pi 27 47
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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