JApplet javax.swing.JApplet Applets are Java programs that are embedded in web pages. When a Java enabled web browser loads a web page containing an applet, the applets downloads into the browser and executes. Java enabled web browsers contain Java Plugin . You can visit www.java.com whether your browser contain Java Plugin or not. If not, you can click the Free Java Download button to install it. The application in which an applet executes is known as applet container . The JDK includes appletviewer applet container for testing applets as you develop them and before you embed them in web page. Every Java applet must contain at least one public class declaration which also extends Applet( java.applet.Applet )or JApplet ( javax.swing.JApplet ). Java applet must override methods init , start, paint, stop, destroy. If any of these methods are not declared according to functionality, applet container calls their inherited empty body versions. After creating applet file having extension .java, create . htm or .html file which contains APPLET tag specifying applet .class file and its path by code attribute. If both files are in same folder, no need to mention path. CODE contains only the name of the class file; CODEBASE contains an alternate pathname where classes are contained. For example, if you store your class files in a directory called /classes, which is in the same directory as your HTML files, CODEBASE is the following: <APPLET CODE=” myclass.class ” CODEBASE=”classes” WIDTH=100 HEIGHT=100> </APPLET> Instructor: Tanzila Kehkashan 2
JApplet Executing applet in appletviewer In command prompt, compile applet file with typical javac following command: javac filename.java In command prompt, execute html file by typing following command: appletviewer filename.html Executing applet in web browser Right click on html file, open with Java enabled browser. Or Open browser. From file menu, open html file. Enable Java in Web Browser Chrome versions launching since April 2015, do not support technology that run java in chrome. Run applets in Safari, Firefox, IE, etc. Before running applet, enable Java in your browser. Internet Explorer Click Tools and then Internet Options Select the Security tab, and select the Custom Level button Scroll down to Scripting of Java applets Make sure the Enable radio button is checked Click OK to save your preference Firefox Open the Firefox browser or restart it, if it is already running From the Firefox menu, select Tools, then click the Add-ons option In the Add-ons Manager window, select Plugins Click Java (TM) Platform plugin to select it Check that the option selected is Ask to Activate or Always Activate or on older Firefox versions, click on the Enable button (if the button says Disable, Java is already enabled) Instructor: Tanzila Kehkashan 3