Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

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


Java Tutorials



Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes


[an error occurred while processing this directive]

Why no applets in these notes

These notes were originally written using applets almost entirely. However, all applet examples are being changed to applications.

  • The only really portable applets are written in Java 1.1 because this is the minimum assumption about what is installed on a user's computer. Java has moved far beyond that. I'm not interested in programs that are limited to the older, and seriously inferior, AWT. Yes, Java plugins can be used for the more recent versions, but it can still be a problem. To get around this, many books that teach applets suggest using the appletviewer program thereby negating the supposed applet advantages.
  • In a controlled environment, such as a large company, it's possible to use later versions of applets, but they aren't the best idea for general examples.
  • Many current applets should instead be animated GIFs, JavaScript, or Flash.
  • Assuming the later versions of Java are used, most applet source will look similar to application code. However, there are some divergences that are annoying: the common practice of init instead of a contructors, the fixed size as opposed to using pack, lack of I/O capability (without permissions), ... It's even worse when using Java 1.1 because the lack of a content pane distorts the program.
  • Java WebStart is a better solution to web distributed software.
  • The primary use of Java has turned out to be applications, not applets as predicted by the initial hype.

Converting Applets to Applications

To make an applet into an application, add a main()method to the applet's class -- you don't have to create a new class. It may seem a little strange that adding a main method to a class will make it into an application, but that's the most common way of doing it. An application is any program that has a main() method. This new main() does what a browser does.

The main method should do the following:

  1. Create a window (JFrame) to hold the applet.
  2. Make the window's close box stop the applet.
  3. Create a new applet object, and add it to the window.
  4. Start the applet by calling init(), then start().
  5. Finalize the layout.
  6. Make the window (with the applet in it) visible.


Here is a very small applet, which just displays some text.

import javax.swing.*;

public class SampleApplet extends JApplet {
    public SampleApplet() {
        add(new JLabel("This is an Applet."));

Add a main() method, and it can be used as either an application or an applet.

import java.awt.*;
import javax.swing.*;

public class SampleApplet extends JApplet {
    public static void main(String[] args) {
        JFrame myWindow = new JFrame("SampleApplet Application");
        SampleApplet theApplet = new SampleApplet(); // Create a new object.
        myWindow.setContentPane(theApplet);  // Add to the window.
        //theApplet.init();           // Needed if defined in applet
        //theApplet.start();          // Needed if defined in applet
        myWindow.pack();              // Arrange the components.
        myWindow.setVisible(true);    // Make the window visible.
    // Constructor
    public SampleApplet() {
        add(new JLabel("This is an Applet and and Application!"));
  • You can use this as an applet and the browser will ignore main().
  • Although SampleApplet is the name of the class, we need to create an instance of it. An applet is a kind of panel, so it can be used for the contentPane in a JFrame.
  • Usually only applets which do animation have a start() method. If your applet doesn't have a start() method, you don't need this call, although it won't cause problems because JApplet has defined a version that does nothing. Similarly with init() which is commonly used in applets, altho a constructor would be a better choice. If there is no init(), there's no need to call it.

Some applets may use features that require additional work.

  • An applet can call getCodeBase() to find out where it was loaded from to get additional classes, images, or other data, then use the return value to get more things. If an applet does this, then you must define getCodeBase().
  • If an applet gets parameters from the web page, you will have to provide a getParameter() method.
  • The showStatus() method must be supplied if used by the applet.

The <applet...> HTML tag

You need to specify the following values in an applet tag.

  • width="nn" height="mm", where nn and mm are the width and the height of the area the applet will occupy on the web page. Applets are not resizeable (except when run with the appletview application. But if you're using appletviewer, why are you writing an applet?
  • Class file location. There are several possibities.
    Simple class files
    Specify code="Xxxx.class"
    Jar file
    If you're using packages (eg, with NetBeans), you will need to specify the .jar file as an archive and then give the class which contains the applet, qualified by the package name. Specify code="yourPackage.Xxxx.class" archive="yourPackage.jar"

Example tag

<applet code="mousedemo.MouseTest.class" archive="mousedemo.jar" width="400" height="200"></applet>
Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

i want complete notes of java in Applet,swind,beans

Posted by Saravanan on Saturday, 07.19.08 @ 18:43pm | #68090

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


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

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

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]