A browser with JavaScript enabled is required for this page to operate properly.
Trail: Deployment
Lesson: Java Applets
Getting Started With Applets
Home Page > Deployment > Java Applets

Getting Started With Applets

The HelloWorld applet shown next is a Java class that displays the string "Hello World".


Note: If you don't see the applet running, make sure that you have at least the Java 2 Platform, Standard Edition (J2SE) 1.4.2 release on your client. If not, download and install the latest release of the Java SE Development Kit (JDK).

Note: If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.

Following is the source code for the HelloWorld applet:


import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class HelloWorld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

An applet such as this is typically managed and run by the Java Plug-in software in the browser.

Download source code for the Hello World example to experiment further.


Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

Previous page: Java Applets
Next page: Defining an Applet Subclass