Creating and Using Applets

Team-Fly

In previous chapters we created many applets. Applets are a subclass of the Applet class (java.applet.Applet). The five primary methods that you'll probably want to override when you write an applet are init(), start(), stop(), destroy(), and paint().

  • init(). This method is called only once in an applet's lifetime. You can use init() for data declaration and initialization or for loading fonts and images.

  • start(). This method is called each time the applet is started, including the first time the applet is called and each time the user returns to the Web page containing the applet.

  • stop(). Like the start() method, the stop() method is called each time the applet is stopped. For example, it is called when the user leaves the Web page containing the applet.

  • destroy(). This method does any final cleanup before the applet is unloaded from memory.

  • paint(). This method displays items in the applet window; it can be called as many times as needed. You need to pass an object of Class Graphics to the paint() method; this object is passed to the applet from the browser.

Overriding these five methods leaves you with a basic applet shell that looks like this:

import java.awt.*; import java.applet.Applet; public class AppletSample extends Applet {     public void init(){ . . . }     public void start(){ . . . }      public void stop() { . . . }     public void destroy() { . . . }     public void paint(Graphics g) { . . . } }

Applet Communications

Applets can also communicate with other programs either by calling public methods of applets that are running in the same browser or by using certain API (Application Programming Interface) calls that are available in either the java.net or java.applet packages.

For two applets to communicate, the first applet needs to determine the name of the second. The name of an applet is defined during the creation of the HTML tag that defines the applet as follows:

<APPLET CODE=Test.class><PARAM NAME="NAME" VALUE="Scooby"> </APPLET>

The current name of an applet can be determined by using the following code segment:

appletName = getParameter("NAME");

You can check whether another applet is executing and then execute a method within that applet by using the getApplet() method. You can also query all the applets currently running by using the getApplets() method. An example of the getApplet() method is shown later in this section.

The applet can communicate with the browser and the originating server. You can also accomplish other tasks with it, such as retrieving the current host name and URL, setting the status message or displaying other documents. Some of the more popular classes and methods follow.

  • appletContext.showDocument(). This method tells the browser to either show a new window or replace the current window.

  • getCodeBase().getHost(). This method retrieves the host from which the applet came.

  • appletContext.showStatus(). This method tells the browser which string to show in the status line.

The following example shows many features of applet communication. It consists of one HTML page with two applets in it (see Figure 10.4). The first applet looks for the second applet, and if it is found, all messages are routed to it. The example also demonstrates some applet-to-browser communication and applet-to-server communication by retrieving various system values.

Caution 

Be careful about using the showDocument() method and the showStatus() method in an applet. Many browsers ignore these methods.

click to expand

Figure 10.4: Output from the communication applet example

page1.html

<!-Test HTML page for Applet communications -> <html> <head> <title>Test Page for AWT Applets</title> </head> <body> <applet name="Jsap1007" code="Jsap1007.class" width=400 height=200>  Java not supported with this browser </applet> <applet name="Jsap1008" code="Jsap1008.class" width=400 height=200> </applet> </body> </html>

Jsap1007

 import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; public class Jsap1007    extends Applet                         implements ActionListener {     Applet            app2 = null;     AppletContext      myContext;     TextArea          txta;     Button            but1;     String            myName;          public void init()     {         // Initialize variables and construct applet objects         txta = new TextArea(6,40);         but1 = new Button("Send Message");         add(txta);         add(but1);         but1.addActionListener(this);         // Get Context for this Applet         myContext = getAppletContext();         // Now check if the applet "Jsap1008" is running         myName = getParameter("NAME");         this.message(myName +  " in Init");     }     public void start()     {         this.message(myName + " in Start");     }     public void stop()     {         this.message(myName + " in Stop");     }    public void destroy()     {         this.message(myName + " in Destroy");     }     public void message(String str)     {         if (app2 != null)         {             ((Jsap1008)app2).message(str);         }         else         {             txta.append(str + "\n");         }     } // Handle the button presses     public void actionPerformed(ActionEvent event)     {         Object target = event.getSource();         if (target == but1)         {             app2 = getAppletContext().getApplet("Jsap1008");             if (app2 != null)             {                 txta.append("Jsap1007 messages -> Jsap1008" + "\n");             }             this.message(myName + ":" + getCodeBase().getHost());             this.message(myName + ":" + System.getProperty("java.class.version"));             this.message(myName + ":" + System.getProperty("java.vendor"));             this.message(myName + ":" + System.getProperty("java.vendor.url"));             this.message(myName + ":" + System.getProperty("java.version"));             this.message(myName + ":" + System.getProperty("os.arch"));             this.message(myName + ":" + System.getProperty("os.name"));         }     }     }

Jsap1008

 import java.applet.*; import java.awt.*; import java.awt.event.*; public class Jsap1008    extends Applet {     TextArea    txta;     String        myName;     public void init()     {         // Initialize variables and construct applet objects         txta = new TextArea(6,40);         add(txta);         myName = getParameter("NAME");         // Now check if the applet "Jsap1008" is running         this.message(myName + " in Init");     }     public void start()     {         this.message(myName + " in Start");     }     public void stop()     {         this.message(myName + " in Stop");     }     public void destroy()     {         this.message(myName + " in Destroy");     }     public void message(String str)     {         txta.append(str + "\n");     } }


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net