Building and Running Applets Core Java

The first two programs presented in this book are Java applications, stand-alone program slike any native programs. On the other hand, most of the hype about Java comes from its ability to run applets inside a web browser.

We want to show you how to build and run an applet from the command line. Then wewill load the applet into the applet viewer that comes with the JDK. Finally, we will displayit in a web browser.

First, open a shell window and go to the directory CoreJavaBook /v1ch02 /WelcomeApplet, then enter the following commands:

javac WelcomeApplet.java
appletviewer WelcomeApplet.html

WelcomeApplet applet as viewed by the applet viewer

WelcomeApplet applet as viewed by the applet viewer

The first command is the now-familiar command to invoke the Java compiler. This compiles the WelcomeApplet .java source into the bytecode file WelcomeApplet .class.

This time, however, you do not run the java program. You invoke the applet viewer program instead. This program is a special tool included with the JDK that lets you quickly test anapplet. You need to give this program an HTML file name, rather than the name of a Javaclass file. The contents of the WelcomeApplet.html file.

WelcomeApplet.html

If you are familiar with HTML, you will notice some standard HTML instructionsand the applet tag, telling the applet viewer to load the applet whose code is stored inWelcomeApplet.class. The applet viewer ignores all HTML tags except for the applet tag.

Unfortunately, the browser situation is a bit messy.

  • Firefox supports Java on Windows, Linux, and Mac OS X. To experiment withapplets, just download the latest version, visit http://java.com, and use the version checker to see whether you need to install the Java Plug-in.
  • Some versions of Internet Explorer have no support for Java at all. Others only support the very outdated Microsoft Java Virtual Machine. If you run Internet Explorer, go to http: //java .com and install the Java Plug-in.
  • If you have a Macintosh running OS X, then Safari is integrated with the Macintosh Java implementation, which supports Java SE 5.0 at the time of this writing.

Provided you have a browser that supports a modern version of Java, you can try loading the applet inside the browser.

  1. Start your browser.
  2. Select File -> Open File (or the equivalent).
  3. Go to the CoreJavaBook /v1ch02 /WelcomeApplet directory. You should see the Welcome -Applet .html file in the file dialog. Load the file.
  4. Your browser now loads the applet, including the surrounding text. It will look something.

Running the WelcomeApplet applet in a browser

Running the WelcomeApplet applet in a browser

You can see that this application is actually alive and willing to interact with the Internet.Click on the Cay Horstmann button. The applet directs the browser to display Cay’sweb page. Click on the Gary Cornell button. The applet directs the browser to pop up amail window, with Gary’s e-mail address already filled in.

Notice that neither of these two buttons works in the applet viewer. The applet viewerhas no capabilities to send mail or display a web page, so it ignores your requests. Theapplet viewer is good for testing applets in isolation, but you need to put applets insidea browser to see how they interact with the browser and the Internet.

TIP: You can also run applets from inside your editor or integrated development environment.

In Emacs, select JDE -> Run Applet from the menu. In Eclipse, use the Run -> Run as
-> Java Applet menu option.

Finally, the code for the applet is shown in below. At this point, do not give it morethan a glance. We come back to writing applets later.

WelcomeApplet.java

Here, you learned about the mechanics of compiling and running Java programs.

You are now ready to move on to next section, where you will start learning theJava language.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics