Click here to Skip to main content
15,867,686 members
Articles / Web Development / HTML
Article

'Wait while loading' message for Java applets

Rate me:
Please Sign up or sign in to vote.
3.46/5 (9 votes)
24 May 2000 153.4K   35   29
An article describing JavaScript code that will display a message (i.e. Please Wait...) while a Java Applet is loading. This code is designed to work in both IE and Netscape

Introduction

Ever wish you could replace that unsightly grey box displayed while your Java applet is loading with a message to the user? The following code provides a solution for IE and Netscape. A more complete description follows the code.

<html>
  <head>
    <title>Your Title Here</title>
  </head>

  <script language="JavaScript">

    <!-- Hide script from old browsers

    function init()
    {
      // Microsoft Internet Explorer
      if (document.all)
      {
        document.all.loading.style.visibility="hidden";
        document.all.myapplet.style.visibility="visible";
        document.applets[0].repaint();
      }
      // Netscape Navigator
      else
      {
        document.loading.visibility="hide";
        document.myapplet.visibility="visible";
      }
    }

    // -->
  </script>

  <style type="text/css">

    #loading {
      position:absolute;
      left:150;
      top:200;
    }

    #myapplet {
      position:absolute;
      left:10;
      top:10;
      visibility:hide;
    }

  </style>

  <body onLoad="init()">

    <div id="loading">
      <p>Please wait while Java applet loads...</p>
    </div>

    <div id="myapplet" style="visibility:hidden">
      <applet archive="app1.jar" code="app1.class" align="baseline" width="620" height="442">
        <p>Requires a browser that supports Java.</p>
      </applet>
    </div>

  </body>
</html>

The Solution Explained

First, let's explain exactly what we're trying to do here. We want a message, such as "Please wait while Java applet loads..." to be displayed while the applet is being loaded. This message should appear in the space that will be occupied by the applet, then be replaced by the applet once the applet has loaded.

The code shown above does a number of things to accomplish this.

Two <div> regions are created in the web page. The first contains the text (or image) to display while the applet is loading. The second contains the <applet> tag to load the applet. This second <div> has its visibility style set to hidden. This is required for IE.

<div id="loading">
  <p>Please wait while Java applet loads...</p>
</div>

<div id="myapplet" style="visibility:hidden">
  <applet archive="app1.jar" code="app1.class" align="baseline" width="620" height="442">
  <p>Requires a browser that supports Java.</p>
</applet>
</div>

Style sheets are defined for the two <div> tags. They both need positional styles. The style sheet for the <div> containing the <applet> tag also needs visibility set to hide (this is for Netscape). Positioning will have to be customized to the position of your applet, this is not a general solution.

<style type="text/css">

  #loading {
    position:absolute;
    left:150;
    top:200;
  }

  #myapplet {
    position:absolute;
    left:10;
    top:10;
    visibility:hide;
  }

</style>

A JavaScript function is defined which is run when the page loads (<body onLoad="init()">). Simply, this function hides the <div> containing the message and shows the <div> containing the applet.

<script language="JavaScript">

  function init()
  {
    if (document.all)
    {
      document.all.loading.style.visibility="hidden";
      document.all.myapplet.style.visibility="visible";
      document.applets[0].repaint();
    }
    else
    {
      document.loading.visibility="hide";
      document.myapplet.visibility="visible";
    }
  }

</script>

This method should work equally well to place an image "over" the applet while it's loading.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhi Pin
ehsan136617-Oct-14 20:27
ehsan136617-Oct-14 20:27 
GeneralSimpleInterest Program in Applet Pin
Member 870389717-Apr-13 6:40
Member 870389717-Apr-13 6:40 
ya this code is little bit difficult . so you have to try this deffinately it is useful to you java question bank

import java.awt.*;
import javax.swing.JOptionPane;
import java.applet.Applet;
/*<applet code="”Interest”" width="200" height="100">
<param name="rate" value="monthly" />
*/
public class Interest extends Applet
{
String rate;
public void start()
{
String s=JOptionPane.showInputDialog(“enter principal amount”);
int p=Integer.parseInt(s);
s=JOptionPane.showInputDialog(“enter rate”);
int r=Integer.parseInt(s);
s=JOptionPane.showInputDialog(“enter time”);
int t=Integer.parseInt(s);
rate=getParameter(“rate”);
if(rate.equals(“monthly”))
{
double amount=(p*t*r)/(100*12);
JOptionPane.showMessageDialog(null,”simple interest=”+amount,”simple interst”,JOptionPane.INFORMATION_MESSAGE);
}
else
{
double amount=(p*t*r)/(100);
JOptionPane.showMessageDialog(null,”simple interest=”+amount,”simple interst”,JOptionPane.INFORMATION_MESSAGE);
}
}
}
General&lt;PARAM name=&quot;progressbar&quot; value=&quot;true&quot;&gt; Pin
amattice28-Sep-04 13:14
amattice28-Sep-04 13:14 
QuestionEver tired to show and then hide an applet? Pin
mxc7-Dec-02 20:16
mxc7-Dec-02 20:16 
GeneralLoading message Pin
13-Mar-02 9:00
suss13-Mar-02 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.