Java in a Nutshell
Example Code

The Java programming examples shown here are from the book Java in a Nutshell , by David Flanagan, published by O'Reilly & Associates. The examples were written by David Flanagan, and are Copyright (c) 1996 by O'Reilly and Associates. You may study, use, and modify these examples for any purpose, but note that they are provided with NO WARRANTY express or implied.

You may also download the complete set of examples as a zip file or as a compressed tar file or as a gzipped tar file. Note that a number of the examples here, particularly those in Section 5, do not stand alone--they rely on other examples from the book or on images from subdirectories. Thus, if you want to experiment with the examples, I recommend that you download the entire distribution by ftp, rather than downloading the examples one at a time through your Web browser.

A Note on Bugs and Portability

The example code here has all been tested on my platform. That means that it will probably, but not necessarily, run on your platform--Java is still not as portable as it should be, and there are still buggy implementations out there. If you have trouble with any of these examples, please first try to figure out the problem, and then send e-mail to bookquestions@ora.com. In your e-mail, please be sure to mention what platform you're using (i.e. what type of computer, what version of the operating system, and what vendor and version of Java you are using.) If you have found a workaround to the problem on your platform, please include it so we'll be able to share that workaround with others who have the same problem. You might also want to submit a bug report to the vendor of your Java environment.

The Examples

The examples from the book are all below. Each one has a short description, and a link that will take you to the source code. In addition, there are links that will allow you to run most of the applet examples directly. Enjoy!

David Flanagan
April 5th, 1996


Example 1-2

Scribble -- an applet of intermediate complexity, used as an example in the introductory chapter. Click and scribble in the window.
view source code  run the applet

Example 2-3

throwtest -- an application that demonstrates how to define, throw, and handle exceptions. This application doesn't do anything other than print out some text, but if you might want to study it and play around with it to learn more about how exceptions work in Java. See the usage instructions in the source code.
view source code 

Example 4-1

FirstApplet -- the simplest possible applet; displays "Hello World".
view source code  run the applet

Example 4-2

SecondApplet -- a fancier version of Hello World.
view source code  run the applet

Example 4-3

Scribble -- a simple applet with user interaction. Click and scribble in the window. This applet cannot refresh itself.
view source code  run the applet

Example 4-4

EventTester -- play with the mouse and keyboard in this window. It will tell you what events are generated.
view source code  run the applet

Example 4-5

ColorScribble -- the scribble applet in color. The colors are specified by tags in the HTML file, and are read by the applet code.
view source code  run the applet

Example 4-6

ClearableScribble -- we add a Clear button to the basic Scribble applet.
view source code  run the applet

Example 4-7

Imagemap -- an example of a simple client-side imagemap implmented in Java.
view source code  run the applet

Example 4-8

Animator -- simple animation in Java.
view source code  run the applet

Example 4-9

AudioAnimator -- play a sound and animate.
view source code

Example 4-10

StandaloneScribble -- an applet converted to run as a standalone Java application.
view source code

Example 5-1

InfoDialog -- a dialog box with a message and an Okay button. Suitable for inclusion in applications or applets.
view source code

Example 5-2

YesNoDialog -- a dialog box with a message and two buttons with configurable text. Suitable for inclusion in applications or applets. This example won't run by itself; it must be used within some other application or applet, like Example 5-5.
view source code

Example 5-3

ReallyQuitDialog -- a subclass of the previous example; asks if the user really wants to quit. Suitable for inclusion in applications or applets. This example won't run by itself; it must be used within some other application or applet, like Example 5-5.
view source code

Example 5-4

AllComponents -- a big window with an example of each kind of GUI component supported by Java. Note: This examples relies on many of the other examples in this section, including Example 5-6, which has portability problems on some platforms. See Example 5-6 for details.
view source code

Example 5-5

AllEvents -- a subclass of the previous example that shows how to handle events from each of the GUI components.Note: This examples relies on many of the other examples in this section, including Example 5-6, which has portability problems on some platforms. See Example 5-6 for details.
view source code

Example 5-6

ScrollableScribble -- an example of working with scrollbars. This class was used in Example 5-4 and Example 5-5. Note that there are some portability problems with this example (that may also effect examples 5-4 and 5-5.) view source code
view source code workaround for Cafe

Example 5-7

MultiLineLabel -- an example of creating a custom GUI component. This is the multi-line label component used in the dialog boxes of examples 5-1 and 5-2.
view source code

Example 6-1

FileViewer -- reads the contents of a specified file and displays them in a window.
view source code

Example 6-2

FileLister -- reads the contents of a directory and displays them. Double clicking on an item displays the file or directory contents.
view source code

Example 6-3

FileCopy -- copy a file. Like Unix cp or DOS copy.
view source code

Example 6-4

GrepInputStream -- an example of subclassing FilterInputStream to do custom filtering.
view source code

Example 6-5

Grep -- an example of using a FilterInputStream subclass.
view source code

Example 6-6

Pipes -- a complicated but rewarding example of using pipes in Java.
view source code

Example 7-1

Fetch -- this example demonstrates how you can use the URL.getContent() method to download plain text files (but not HTML or other kinds of text files). Invoke this program with the URL of a text file as its only argument. If you use a file: URL, you may have to give the file a .txt extension so the system recognizes it as a plain text file.

This example also contains a fetchimage() method that uses URL.getContent() to download images in GIF and other common file formats. You can use the separate FetchImageTest class to demonstrate the use of the fetchimage() method.

These plain text and image fetching examples rely on "content handlers" internal to the Java implementation. They work on JDK systems, but may not work on other Java implementations, if those implementations do not include the appropriate content handlers. If you attempt to download a file with an unsupported content type, the example will generate an exception and exit.

Note that this example is intended only to demonstrate the use of URL.getContent(). In general, this is not the best way to load text or image files over the net. See Applet.getImage(), for example.
view source code
view source code for the FetchImageTest class that demonstrates image fetching

Example 7-2

GetURLInfo -- demonstrates how to obtain more information about a URL, and how to have more control over downloading the contents of the URL.
view source code

Example 7-3

UDPSend -- send a datagram.
view source code

Example 7-4

UDPReceive -- receive a datagram.
view source code

Example 7-5

Server -- a server that can accept connections from any number of clients. This example provides the service of reversing the characters on each line it reads and sending the reversed lines back.
view source code

Example 7-6

Client -- an example client to work with the previous example.
view source code

Example 7-7

AppletClient -- a client implemented as an applet. You cannot directly run this applet here because the server (Example 7-5) is not running. To test this applet, bear in mind that for reasons of applet security, the server must be running on the same machine that the applet is loaded from.

A number of people have reported having trouble getting this example to work, and I have not been able to diagnose the problem. The applet does at least work with the Linux port of the 1.0.1 JDK, when both the server and applet are running on the local machine. If this applet does not work for you, it may be that your Java implementation is at fault. Networking is one of the areas in which Java is not yet as portable as it should be.
view source code

Example 8-1

Smooth -- demonstration of techniques for smooth animation
view source code  run the applet

Example 8-2

Animator2 -- a better animator applet. Uses MediaTracker.
view source code  run the applet

Example 8-3

GrayButton -- an example of image processing with the RGBImageFilter class.
view source code run the applet

Example 9-1

ThreadLister -- list all the threads and threadgroups running in the interpreter.
view source code

Example 9-2

AppletThreadLister -- an applet version of the previous example.
view source code  run the applet

Example 9-3

Server -- another Server example with more sophisticated thread usage.
view source code

Example 9-4

Client -- another client example also with more sophisticated thread usage.
view source code