Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Applet

                         

Introduction
Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining. 

Advantages of Applet:

  • Applets are cross platform and can run on Windows, Mac OS and Linux platform
  • Applets can work all the version of Java Plugin
  • Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security approval
  • Applets are supported by most web browsers
  • Applets are cached in most web browsers, so will be quick to load when returning to a web page
  • User can also have full access to the machine if user allows

Disadvantages of Java Applet:

  • Java plug-in is required to run applet
  • Java applet requires JVM so first time it takes significant startup time
  • If applet is not already cached in the machine, it will be downloaded from internet and will take time
  • Its difficult to desing and build good user interface in applets compared to HTML technology

Life Cycle of Applet: Applet runs in the browser and its lifecycle method are called by JVM when it is loaded and destroyed. Here are the lifecycle methods of an Applet: 

init(): This method is called to initialized an applet

start(): This method is called after the initialization of the applet.

stop(): This method can be called multiple times in the life cycle of an Applet.

destroy(): This method is called only once in the life cycle of the applet when applet is destroyed.

init () method: The life cycle of an applet begins when the applet is first loaded into the browser and called the init() method. The init() method is called only one time in the life cycle on an applet. The init() method is basically called to read the PARAM tag in the html file. The init () method retrieve the passed parameter through the PARAM tag of html file using get Parameter() method All the initialization such as initialization of variables and the objects like image, sound file are loaded in the init () method .After the initialization of the init() method user can interact  with the Applet and mostly applet contains the init() method.

Start () method: The start method of an applet is called after the initialization method init(). This method may be called multiple times when the Applet needs to be started or restarted. For Example if the user wants to return to the Applet, in this situation the start Method() of an Applet will be called by the web browser and the user will be back on the applet. In the start method user can interact within the applet.

Stop () method:  The stop() method can be called multiple times in the life cycle of applet like the start () method. Or should be called at least one time. There is only miner difference between the start() method and stop () method. For example the stop() method is called by the web browser on that time When the user leaves one applet to go another applet and the start() method is called on that time when the user wants to go back into the first program or Applet.

destroy() method: The destroy() method is called  only one time in the life cycle of Applet like init() method. This method is called only on that time when the browser needs to Shut down.

Creating First Applet Example: First of all we will know about the applet. An applet is a program written in java programming language and embedded within HTML page. It runs on the java enabled web browser such as Netscape navigator or Internet Explorer.

In this example you will see, how to write an applet program. Java source of applet is then compiled into java class file and we specify the name of class in the applet tag of html page. The java enabled browser loads class file of applet and run in its sandbox.

Here is the java code of program :

import java.applet.*;
import java.awt.*;

public class FirstApplet extends Applet{
  public void paint(Graphics g){
    g.drawString("Welcome in Java Applet.",40,20);
  }
}

Here is the HTML code of the program:

<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET
ALIGN="CENTER" CODE="FirstApplet.class" WIDTH="800" HEIGHT="500"></APPLET>
</BODY>
</HTML>

Passing Parameter in Java Applet: Java applet has the feature of retrieving the parameter values passed from the html page. So, you can pass the parameters from your html page to the applet embedded in your page. The param tag(<parma name="" value=""></param>) is used to pass the parameters to an applet. For the illustration about the concept of applet and passing parameter in applet, a example is given below.

In this example, we will see what has to be done in the applet code to retrieve the value from parameters. Value of a parameter passed to an applet can be retrieved using getParameter() function. E.g. code:

String strParameter = this.getParameter("Message");

Printing the value:

Then in the function paint (Graphics g), we prints the parameter value to test the value passed from html page. Applet will display "Hello! Java Applet"  if no parameter is passed to the applet else it will display the value passed as parameter. In our case applet should display "Welcome in Passing parameter in java applet example." message.

Here is the code for the Java Program : 

import java.applet.*; 
import java.awt.*; 
             
public class appletParameter extends Applet {
  
private String strDefault = "Hello! Java Applet.";
  
public void paint(Graphics g) {
    String strParameter = 
this.getParameter("Message");
   
 if (strParameter == null)
    strParameter = strDefault;
    g.drawString(strParameter, 50, 25);
  } 
}

Here is the code for the html program : 

<HTML>
<HEAD>
<TITLE>
Passing Parameter in Java Applet</TITLE>
</HEAD>
<BODY>

This is the applet:<P>
<APPLET code="appletParameter.class" width="800" height="100">
<PARAM name="message" value="Welcome in Passing parameter in java applet example.">
</APPLET>
</BODY>
</HTML>

There is the advantage that if need to change the output then you will have to change only the value of the param tag in html file not in java code.

Compile the program : 
javac appletParameter.java

Output after running the program : 
To run the program using appletviewer, go to command prompt and type appletviewer appletParameter.html Appletviewer will run the applet for you and and it should show output like
Welcome in Passing parameter in java applet example. Alternatively you can also run this example from your favorite java enabled browser.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
java combo box
Linux Caixa M�?�а
spring hibernate jsf1.
exceptions in java
JMS Tutorials
Photoshop Text Effects
tree and treenode (Aja
bean:message
change color in JTabl
breakà¹?à¸??à¹?à¸????à
reading a CSV file in
ASP.NET .NET Inside AS
space in bytes for arr
â?¨Ð°â?¨????????â?¨Ð°â
DDl and DMl statements
Flash Dynamic C...de/d
ะ��???ะ�ั?
Overview of Networking
Dependency list box in
Combo Box operation in
Snow
Java аÐ?б?а??аÐ?б
PHP Site Navigation Ad
register
Programming in C langu
Custom Rendering of JL
send emails with jsp
java beans
Murach???????????????à
java.oi.package progra
reset commandbutton
runnable
Visual Basic TCP/IP Pr
subquery in HQL
save form data in text
Photoshop Effects Clou
socket
list jsp
mxml
json lib//componen...]
pdf box get text
struts language swit
write a swing applicat
how to override a vari
passing values from js
filters
decimal to ANSI
java mail to more rece
print a new ln
spring hybernate eclip
give me some java pro
table j2me
digital numbers recogn
à®°???????à®°??????à®°
palindrome checking th
Photoshop PhotoChoppi
IllegalStateException
Photoshop Photo Effect
httpgetexample
how to access mysql da
Java Data Objects
JSp:Input Parameter
searchUser.jsp
Java I/O
javacombobox
getwrapped
titles
reduce file size
javascript to move a t
field validator
Get date of first day
on clikck radio struts
Enabling and Disabling
getchars
Cinema 4D Model a Glas
JText
How to Validate a File
PhotoshopEffectsHolein
break?ல??ய�ல??
insert data todabase u
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.