Web Hosting Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting

Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses.


Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Web Hosting Welcome Web Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Web Hosting Fund Raising, Fundraising, web hosting Web Hosting Resellers, web Hosting Web Hosting Web Design, web Hosting Web Hosting Extra Services,  web Hosting Web Hosting Traffic Booster, web hosting Web Hosting Traffic Booster, web hosting Web Hosting Technical Support,  web Hosting Web Hosting webmaster tips,  web Hosting Web Hosting 30 Day Money Back, web hosting Web Hosting Legal Notices for Web Hosting Web Hosting Glossary Computer Terms for web Hosting Web Hosting Contact Information - web hosting

Site Map

  Web Hosting Web Hosting Sign-Up   Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting    Web Hosting Resellers web hosting, website hosting, web site hosting   Web Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Getting Started with Applets (The Java™ Tutorials > Deployment > Applets)
Trail: Deployment
Lesson: Applets
Home Page > Deployment > Applets
Getting Started with Applets

Below is the HelloWorld applet, which is a simple Java class that prints the string "Hello World" in small rectangle.

Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the Java(TM) SE JRE or JDK. This applet requires JDK 1.4 or later. You can find more information on the Java Plug-in home page.

We strongly recommend that you install the latest version; release 1.4 or later is required for all our applets. You can find more information in Troubleshooting Applet Problems

Following is the source code for the HelloWorld applet:

import javax.swing.JApplet;
import java.awt.Graphics;

public class HelloWorld extends JApplet {
    public void paint(Graphics g) {
	g.drawRect(0, 0, 
		   getSize().width - 1,
		   getSize().height - 1);
        g.drawString("Hello world!", 5, 15);

An applet such as this is typically managed and run by Java Plug-in. Java Plug-in, which is automatically included when you download the Java(TM) SE Runtime Environment (JRE), extends the functionality of a web browser, allowing applets to be run under Sun's Java(TM) SE Runtime Environment (JRE) rather than the Java Runtime Environment that comes with the web browser. It works with the Mozilla family of browsers and with Internet Explorer.

Converting Applications to Applets

An application is a standalone program consisting of at least one class with a main method. Applets differ significantly from applications. First, applets do not have a main method that is automatically called to begin the program. Instead, several methods are called at different points in the execution of an applet. The difference between Java applets and applications lies in how they are run. Applications are usually run by loading the application's main class file with a Java interpreter, such as the java tool in the JDK(TM) 6.

Basic steps to follow to convert an application program into an applet program:

  • You need to create a subclass of java.applet.Applet in which you override the init method to initialize your applet's resources the same way the main method initializes the application's resources.
  • init might be called more than once and should be designed accordingly. Moreover, the top-level Panel needs to be added to the applet in init; usually it was added to a Frame in main. That's it!

    You may understand clearly as to how you can convert an application program into an applet, by going through a sample application program SwingUI.java here and its corresponding applet program ApptoAppl.java

    When you compare these two programs, you may come up with the following major differences between the two:

  • The applet class is declared public so appletviewer can access it.
  • The applet class descends from Applet/JApplet and the application class descends from Frame/JFrame.
  • The applet version has no main method.
  • The application constructor is replaced in the applet by start and init methods.
  • GUI components are added directly to the Applet; whereas, in the case of an application, GUI components are added to the content pane of its JFrame object.

    The rest of this section contains the following to help you get started with applets:

  • Extending Applet or JApplet

    Every applet must define a subclass of the Applet or JApplet class. In the "Hello World" applet, this subclass is called HelloWorld. Applets inherit a great deal of functionality from the Applet or JApplet class, including abilities to communicate with the browser and present a graphical user interface (GUI) to the user.

    The Life Cycle of an Applet

    The HelloWorld applet implements just one method, the paint method. Every applet must implement at least one of the following methods: init, start, or paint. This section introduces a new applet, Simple, that uses all of these methods. Unlike Java applications, applets do not need to implement a main method.

    Methods for Milestones

    The JApplet class provides a framework for applet execution, defining methods that the system calls when milestones — major events in an applet's life cycle — occur. Most applets override some or all of these methods to respond appropriately to milestones.

    Methods for Drawing and Event Handling

    Applets inherit the drawing and event handling methods of the AWT Component class. Drawing refers to anything related to representing an applet on-screen — drawing images, presenting user interface components such as buttons, or using graphics primitives. Event handling refers to detecting and processing user input such as mouse clicks and key presses, as well as more abstract events such as saving files and iconifying windows.

    Methods for Adding UI Components

    Applets inherit from the AWT Container class. This means that they are designed to hold Components — user interface objects such as buttons, labels, pop-up lists, and scrollbars. Like other Containers, applets use layout managers to control the positioning of Components.

    What Applets Can and Can't Do

    For security reasons, applets that are loaded over the network have several restrictions. One is that an applet can't ordinarily read or write files on the computer that it's executing on. Another is that an applet can't make network connections except to the host that it came from. Despite these restrictions, applets can do some things that you might not expect. For example, applets can invoke the public methods of other applets on the same page.
    Previous page: Applets
    Next page: Importing Classes and Packages for Applets
    Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
    Add to My Yahoo!

    XML icon

    Add to Google












    JSP at alden-servlet-Hosting.com
    Servlets at alden-servlet-Hosting.com
    Servlet at alden-servlet-Hosting.com
    Tomcat at alden-servlet-Hosting.com
    MySQL at alden-servlet-Hosting.com
    Java at alden-servlet-Hosting.com
    sFTP at alden-servlet-Hosting.com
    JSP at alden-tomcat-Hosting.com
    Servlets at alden-tomcat-Hosting.com
    Servlet at alden-tomcat-Hosting.com
    Tomcat at alden-tomcat-Hosting.com
    MySQL at alden-tomcat-Hosting.com
    Java at alden-tomcat-Hosting.com
    sFTP at alden-tomcat-Hosting.com
    JSP at alden-sftp-Hosting.com
    Servlets at alden-sftp-Hosting.com
    Servlet at alden-sftp-Hosting.com
    Tomcat at alden-sftp-Hosting.com
    MySQL at alden-sftp-Hosting.com
    Java at alden-sftp-Hosting.com
    sFTP at alden-sftp-Hosting.com
    JSP at alden-jsp-Hosting.com
    Servlets at alden-jsp-Hosting.com
    Servlet at alden-jsp-Hosting.com
    Tomcat at alden-jsp-Hosting.com
    MySQL at alden-jsp-Hosting.com
    Java at alden-jsp-Hosting.com
    sFTP at alden-jsp-Hosting.com
    JSp at alden-java-Hosting.com
    Servlets at alden-java-Hosting.com
    Servlet at alden-java-Hosting.com
    Tomcat at alden-java-Hosting.com
    MySQL at alden-java-Hosting.com
    Java at alden-java-Hosting.com
    sFTP at alden-java-Hosting.com
    JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . .
    . .
    . . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting