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, 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 not provided with any guarantee or 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.

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 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.
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.
view source code

Example 5-4

AllComponents -- a big window with an example of each kind of GUI component supported by Java.
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.
view source code

Example 5-6

ScrollableScribble -- an example of working with scrollbars. This class was used in Example 5-4. Note that scrollbars work differently and incompatibly on Unix and Windows systems. This example works on Unix systems, but not Windows.
view source code

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 -- downloading the contents of a text file URL.
view source code

Example 7-2

GetURLInfo -- getting more information about a URL, and more control over downloading it.
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.
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

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