Applet Basics

     

Applets are regular Java classes that extend Applet. Because of this, you can do just about anything that Java allows you to do from within an applet that runs in its own space on the client. There are, however, key restrictions. For example, you are not allowed to open a socket connection from inside your applet, except one back to the same server from which the applet came. Additionally, you cannot access the local file system. Beyond constraints such as these, applets are very powerful.

There are a few steps to getting an applet running on a client.

  • Write a Java class that extends the java.applet.Applet class or javax.swing.JApplet class and imports the packages you need.

  • Write an HTML page that calls the applet using the HTML <object> tag or <applet> tag.

Using the HTML <object> tag, you specify the size and location of the applet on the page, as well as the class file to be used. The browser then loads the plug-in, which contains its own Java Virtual Machine.

In the beginning, you could only view an applet by viewing the Web page in Sun's HotJava browser. This browser is currently in version 3.0 and is still available for free download. However, it is not a full-featured browser, it is slow, and it has little support for the kinds of developments in the language (such as CSS) that users have come to expect.

graphics/fridge_icon.jpg

FRIDGE

You should already have the plug-in installed. It comes with the JRE. If for some reason you don't, you need to download it from www.java.com to execute the examples in this section. If you do have it installed, you can open the plug-in outside a browser and review the options it makes available to you. On Windows machines, you access it by double-clicking the icon in the Control Panel. You can also try opening Internet Explorer and opening the Tools menu. You should see "Sun Java Console" there.


Applets moved into the big time when they were adopted by Internet Explorer and Netscape browsers. Using a virtual machine embedded in the browser itself, applets could be embedded in a Web page using the HTML <applet> tag. As of HTML 4.0, the W3C has deprecated this tag in favor of <object> , which is more general. Note that older browsers may not recognize the <object> tag. Browser manufacturers have not updated their Java Virtual Machines for a version later than Java 1.1. This poses obvious difficulties to developers interested in deploying applets with functionality defined in later versions of Java.

So, Sun devised a plan to propagate use of applets without being at the mercy of Netscape and Microsoft to keep up-to-date. This plan became the Java plug-in. You are likely familiar with this plug-in if you have used the < cfform > controls < cftree > , <cfgrid> , and so forth in ColdFusion 5 or later. This 5MB download allows both Internet Explorer and Netscape to execute applets using this runtime, which should always be up-to-date. Another benefit to the plug-in is added user control over the execution environment. The plug-in allows users to switch between different versions of Java Virtual Machines, for instance.

Eventually, Sun determined that limiting the plug-in to working with the newer <object> tag was dissuading developers from using applets. As of now, the fate of applets is admittedly up in the air.

If you run an applet, you should see the small Java coffee cup logo in your system tray. This indicates that an instance of the Java applet plug-in is running. You can right-click on this icon and choose Open Console to view information regarding your running applet. If the applet has errors, they will show up here, which is good to know for debugging.

The other option of interest in the Java plug-in is the Control Panel, as shown in Figure 29.1. Click Open Control Panel to bring it up.

Figure 29.1. The Java applet control panel offers a way to tinker with network settings and automatic updates.

graphics/29fig01.gif


Here you can view your security settings and fine tune how applets are run in your browser.

There are basically two options for writing applets. You can extend java.applet.Applet. You can also choose to write applets using Swing components, which are sophisticated GUI elements located in the javax.swing package. To write applets using Swing components , you instead extend javax.swing.JApplet.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

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