|Navigate User Interface topic:
A Java applet is an applet delivered in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Oracle's AppletViewer, a stand alone tool to test applets. Java applets were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython.
Applets are used to provide interactive features to web applications that cannot be provided by HTML. Since Java's bytecode is platform independent, Java applets can be executed by browsers for many platforms, including Windows, Unix, Mac OS and Linux. There are open source tools like applet2app which can be used to convert an applet to a stand alone Java application/windows executable. This has the advantage of running a Java applet in off-line mode without the need for Internet browser software.
The two things you must at least create is an HTML page and a Java class. It can be done on a local folder, no need to run a server but it will be harder to understand what is local, what is remote. The HTML page has to call the Java class using the
|Code listing 9.3: HelloWorld.html
Save this file on a folder. As the
<applet/> markup is calling a Java class called
HelloWorld, our class should be called
|Code listing 9.4: HelloWorld.java
Save this file and compile the class on the same folder. Now let's open the web page on a browser:
We clearly see that "Hello, world!" is not rendered the same way as the rest of the page.
- See also applet markup.
To embed an applet in a HTML page, you have to insert a
<applet/> markup. This markup can have several attributes:
||The name of the main class to call. It could be the name of the class with or without the
||The height of the area where the content of the applet can be rendered on the web page.|
||The width of the area where the content of the applet can be rendered on the web page.|
||The name of a compressed zip archive having
The attributes with * are mandatory.
There have been some discussions about the usage of
applet tag but it still can be used for beginning and also would work in the real world as well.
Java source codeEdit
Applets are not constructed in the same way as other classes or main programs. The entry point is different and the main class should extend the
Applet class. The
Applet class has four methods that can be called by the browser and you can redefine:
||Called when the browser first loads the applet. It is only called once by browser execution.|
||Called when the applet starts running. It is called as many times as the user visits the web page.|
||Called when the applet stops running. It is called as many times as the user visits the web page.|
||Called when the user quits the browser. It is only called once by browser execution.|
||Called when the applet needs to be rendered, for example, when the browser is resized.|
The four first methods define the lifecycle of an applet. At least
paint() must be redefined. The HTML applet tag can be embedded in the applet source code to allow the applet to be run directly by a simple applet viewer, without the need for an .html file. Typically, the applet tag immediately follows the import statements. It must be enclosed by
/* */ comments:
|Code section 9.10: MyApplet comment