IMPORTANT: The sample applet used in this article was written and compiled using Swing 1.1.1 beta 1. You can run it by using Swing 1.1 beta 3 or later, or by downloading and using JDK 1.2 or later. How Java Plug-in WorksIf you open a Web page that contains a Java Plug-in-powered applet, and you have Java Plug-in installed on your computer system, you don't have to do anything special to activate the applet: It simply opens and runs automatically. You don't even have to worry about how your system's CLASSPATH variable is set, because Java Plug-in works independently of such things as environment variables. If you open a Web page that contains a Java Plug-in-powered applet, and you don't have Java Plug-in installed, a dialog box opens automatically, and you get an invitation to download a special Java Plug-in engine from Sun. (Of course you must be connected to the Internet for this invitation to appear.) If you agree to have Java Plug-in installed on your system, an installer application runs automatically, and from then on, you're a Java Plug-in user; until you change computers, you'll never have to install Java Plug-in again. To create applets that make use of Java Plug-in, and to place them on Web pages that you publish, you have to know something about how Java Plug-in works, and what happens when a user opens a Web page that contains an applet powered by Java Plug-in. This article provides all the information you'll need about how Java Plug-in works. It describes a couple of different methods for distributing and activated applets powered by Java Plug-in, and it concludes with a hands-on demonstration of Swing applet powered with Java Plug-in. This section also contains two related articles:
Java Plug-in Isn't the Only Answer Java Plug-in, as powerful as it is, may not meet the needs of every applet that you want to distribute. For one thing, Java Plug-in is designed to be downloaded from a network, but some computers don't have network connections. And you might find it difficult to adapt Java Plug-in to certain combinations of browsers and operating systems that are sometimes found in multiuser corporate computer environments. In situations such as these, several alternatives to using Java Plug-in are available. To learn how to implement some of them, read the article in this section titled "Make Your Browser Swing!".
Using Java Plug-in There's nothing special about the way a Java Plug-in applet is written: Any applet can be accessed, loaded, and executed using Java Plug-in. All you need to run an applet using Java Plug-in is some special HTML code for the Web page on which the applet appears.
First, though, we'll cover some basic information about how Java Plug-in works, and what developers have to do to create and distribute applets using Java Plug-in.
About Java Plug-in The Java Plug-in software package is a mechanism for creating and distributing applets written in the Java programming language. Java Plug-in lets you distribute applets using Swing in such a way that the applets will run automatically on recent versions of either of the two leading-brand browsers (Netscape or Internet Explorer). Java Plug-in technology is particularly useful for distributing applets written with Swing because not all browsers have Swing support built in. Java Plug-in is implemented as a downloadable JRE (Java Runtime Engine). To use Java Plug-in with Swing, all you have to do is write an applet that makes use of Swing components, and then add some special HTML code to the Web page that contains your applet. Then, when the user of a Netscape or Internet Explorer browser opens the page containing your applet, one of two things can happen, depending on the state of the user's system. If the reader of your Web page has never run an applet using Java Plug-in, some prompts pop up, followed by a network download (if the user OKs one). The Plug-in code needed to run your applet is then downloaded, and your applet executes automatically. From that point on, the user's system will automatically run any applet that is powered with Java Plug-in -- without presenting any more dialogs or performing any more downloads. Once the Java Plug-in JRE is installed, a Java Plug-in applet runs just like any other applet. Internally, applets powered by Java Plug-in use the runtime that's downloaded with Java Plug-in instead of using the default Java virtual machine that's built into the user's Netscape or Internet Explorer browser. If you'd like more details about Java Plug-in, you can find a wealth of general and technical information at the Java Plug-in home page.
Java Plug-in and Browser Versions
For more information about versions of Netscape browsers and how they work with Swing, see the section headed "Versions of Netscape browsers" in the article in this section titled "Make Your Browser Swing!".
A Brief History of Java Plug-in Sun announced the premiere of Java Plug-in on April 30. The product is available now as a free download from the Java Plug-in Web page. Java Plug-in contains the Swing component set as part of its core. Consequently, when you download Java Plug-in, you also get Swing. From then on, you can use Swing in any applets that you distribute using Java Plug-in. Future releases of the Java Plug-in software will also bundle other Java platform products, including Java Development Kit 1.2 and the ultra-fast Java HotSpot virtual machine. Apple and Linux, too Apple Computer has announced plans to port Java Plug-in to Netscape Navigator on the Macintosh, delivering full Java programming language compatibility to enterprises running Navigator on Macintosh desktops. Also, independent Linux developers have created a runtime Java Plug-in for Linux. It can be downloaded at no cost from www.blackdown.org/java-linux.html.
Creating a Java
Plug-in applet
On an HTML page, the only thing that distinguishes a Java Plug-in applet from any other applet is a difference in tagging. When you place a Java Plug-in applet on a Web page, you don't use the familiar <APPLET> and </APPLET> tags. Instead, you must use special kinds of tags and a special set of syntax rules. If you aren't interested in writing your own Java Plug-in HTML code, but would rather let the Java HTML Converter tool to generate it for you, you needn't bother to read the rest of this article. Instead, feel free to jump to the article titled "Using The Java Plug-in Converter," which shows how you can use the Converter tool to generate your Java Plug-in HTML code automatically. If you do want to learn more about how Java Plug-in works, and would like to learn how to write Java Plug-in HTML code yourself, read on.
Using Java Plug-in with IE BrowsersBefore we begin our discussion of how to write Java Plug-in HTML code, there's one important fact you should be aware of. Because the two leading brands of browsers implement plug-ins in different ways, you have to more than just substitute a different tag for the <APPLET> and </APPLET> tags when you want to implement a Java Plug-in applet. Instead, there are two different conventions for implementing Java Plug-in applets on HTML pages, and you must learn both of them if you want to build Java Plug-in power into the applets you create and distribute. First, let's assume that you want to implement a Java Plug-in applet in a Web page that will be run only on Internet Explorer browsers -- never on Netscape Navigator browsers. That is an unlikely scenario, of course -- it's far more likely that you'll want your applets to run on both kinds of browsers. But let's take one step at a time, and assume for now that we're interested only in the Internet Explorer browser. Later in this article, we'll expand our horizons to include both the IE and the Netscape Navigator (or Communicator) browsers. The <OBJECT> and </OBJECT> Tags To embed a Java Plug-in applet in a Web page that is destined to be displayed using an Internet Explorer browser, you must delimit the code that implements your applet with the tags <OBJECT> and </OBJECT>, instead of with the more familiar tags <APPLET> and </APPLET>. Example 1 shows how the <OBJECT> and </OBJECT> tags might be used to execute a Java Plug-in applet from a page displayed on an Internet Explorer browser. Example
1
Syntax notes As Example 1 illustrates, the syntax used by the <OBJECT> tag is a little different from the syntax that the <APPLET> tag requires. One important difference is that the <OBJECT> tag expects you to use the construct PARAM NAME="xxx" VALUE="xxx" to pass parameters to your applet. In Example 1, the parameters passed to the PlugInApp program are: <PARAM NAME="code" VALUE="PlugInApplet.class"> <PARAM NAME="codebase" VALUE="../src/"> Using the <OBJECT> tag You may also notice that in Example 1, the <OBJECT> tag itself contains a series of interesting elements:
width="120" height="160" align="left"
That's all we'll have to say about the <OBJECT> tag right now.
Using Java Plug-in
with Netscape Browsers
Now that you know how to incorporate a Java Plug-in applet into an Internet Explorer browser, let's take a brief look at a similar procedure for implementing a Java Plug-in applet from a Netscape Navigator browser. When we've finished doing that, we'll combine both techniques and see what it takes to create and distribute a Java Plug-in applet that can be executed from either kind of browser. If you want to equip an HTML page with a Java Plug-in applet that be executed from a Netscape Navigator browser, you must use a completely different pair of tags: <EMBED> and </EMBED>. Example 2 is a code fragment that shows how you can use the <EMBED> tag to implement a Java Plug-in applet from a page displayed by a Netscape Navigator browser. Example
2 <EMBED
type="application/x-java-applet;version=1.2" If you understood the code presented in Example 1, you should have no trouble understanding code shown in Example 2. But there are a couple of new points worth mentioning:
Running
Java Plug-in Applets
So far, we have seen how to implement a Java Plug-in applet on an Internet Explorer browser, and how to embed the same applet in a page running on a Netscape Navigator browser. Now, as promised, we'll show how you can create and distribute Java Plug-in applets that will run on either kind of browser. To learn how to pull this off, there are two facts you must remember:
You'll learn why these two facts are significant -- and why they are important to Java Plug-in programmers -- under the subheadings that follow. Navigator Ignores the <OBJECT> Tag As mentioned earlier, when you want to implement a Java Plug-in applet in HTML code that is destined to be run on an Internet Explorer browser, you substitute the <OBJECT> and </OBJECT> tags for the familiar <APPLET> and </APPLET> tags. That brings up an important question: What happens when you implement a Java Plug-in applet on an HTML page using the <OBJECT> tag and then someone decides to run the page on a Netscape Navigator browser? The answer is -- nothing. Why not? Because, as mentioned previously, Netscape Navigator ignores the <OBJECT> tag and everything inside it. Consequently, whenever you want to implement a Java plug-in applet from an HTML page, you can safely do so by using the <OBJECT> and </OBJECT> tags. When you do that, your applet's implementation code will execute when your HTML page is displayed on an Internet Explorer browser, but will be ignored (safely) when your HTML page is loaded into a Netscape Navigator browser. So now you know how to display a Java Plug-in applet on IE. All that's left to be concerned with now is how to get the same thing to happen when your HTML page is displayed on Netscape Navigator (or Communicator) browser. IE Doesn't Ignore the <EMBED> Tag As mentioned earlier, when you want to implement a Java Plug-in applet from a Netscape Navigator browser, the tags you must use to delimit your applet are <EMBED> and </EMBED>. You may be able to guess the next question that arises: What happens when you implement a Java Plug-in applet on an HTML page using the <EMBED> tag and then someone decides to run the page on an Internet Explorer browser? Don't try to answer this one too quickly, because the answer is -- the results are not defined. Why not? Because Internet Explorer doesn't treat Navigator's <EMBED> tag nearly as nicely as Navigator treats IE's <OBJECT> tag. You can't just place a Java Plug-in applet between a pair of <EMBED> and </EMBED> tags and expect IE to do the right thing. IE doesn't recognize the <EMBED> and </EMBED> tags, and that's why the results of using them are not predictable. Fortunately, though, there is a large loophole that you can drive your Java Plug-in applet through to remedy this situation. That loophole is called the <COMMENT> tag. You can use the <COMMENT> and </COMMENT> tags with Internet Explorer because IE ignores the <COMMENT> tag, the </COMMENT> tag, and everything that appears between them. So if you use the <COMMENT> to comment out everything everything that's intended for Netscape eyes only, you can safely use the <EMBED> and </EMBED> tags to implement Java Plug-in applets that will be processed by Netscape Navigator (or Communicator) and will be ignored by IE browsers. Example 3 shows how to implement a Java plug-in applet that will run correctly on both kinds of browsers because of the way it uses the <OBJECT>, <EMBED>, and <COMMENT> tags. Example
3 <OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" In this example, Internet Explorer recognizes everything that appears between the <OBJECT> and </OBJECT> tabs, except for the material delimited by the <COMMENT> and </COMMENT> tags. Netscape Navigator ignores the <OBJECT> tag and all the <PARAM NAME> tags that follow it, but processes everything that appears between the <EMBED> and </EMBED> tags.
Conclusion
So there you have it! An applet that works under both popular brands of browsers: Netscape Navigator and Internet Explorer. Because the purpose of this article was to teach you some of the basic theory behind Java Plug-in, it showed you some techniques that are a little complicated. To learn about an easier way to use Java Plug-in, see the article in this section titled "Using the Java Plug-in Converter."
| |||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||