Examples From Java in a Nutshell, Second Edition
Although you can view the example source code online, by following the links below, I recommend that you download the complete set of examples so that you can work with them on your computer locally. They are available as a zip file or as a gzipped tar file.
Reporting BugsIf you find any bugs in these examples, please send e-mail describing the bug to email@example.com. If you have found a workaround to the problem, please include it.
CopyrightThe examples were written by David Flanagan, and are Copyright (c) 1997 by O'Reilly and Associates.
You may study, use, and modify these examples for any purpose. This means that you can use the examples, or modified versions of the examples in your programs, and you can even sell those programs. You can distribute the source code to these examples, but only for non-commercial purposes, and only as long as the copyright notice is retained. This means that you can make them available on a public Web site, for example, but that you cannot include them on a commercial CD-ROM without the prior permission of O'Reilly and Associates.
Note that these examples are provided AS-IS, with absolutely NO WARRANTY of any kind, either expressed or implied.
Example 1-2Scribble.java: an applet of intermediate complexity, used as an example in the introductory chapter.
Example 2-3throwtest.java: an application that demonstrates how to define, throw, and handle exceptions. This application doesn't do anything other than print out some text, but 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.
Example 6-1FirstApplet.java: the simplest possible applet. Displays "Hello World"
Example 6-2SecondApplet.java: a fancier version of "Hello World"
Example 6-3Scribble.java: a simple applet with user interaction. It allows the user to click and scribble in the window.
Example 6-4ColorScribble.java: the scribble applet, with colors specified through applet parameters in an HTML file.
Example 6-5Soundmap.java: An applet that displays an image, plays a sound, and demonstrates several other applet capabilities.
Example 7-1Scribble1.java: a simple applet, using the Java 1.0 event model.
Example 7-2Scribble2.java: the same applet, using the Java 1.1 event model.
Example 7-3Scribble3.java: the applet using the Java 1.1 event model and inner classes.
Example 7-4Scribble4.java: the applet using a low-level interface to the Java 1.1 event model.
Example 8-1ScribbleFrame.java: a relatively long application that demonstrates many of the new AWT features of Java 1.1, and also demonstrates object serialization and data compression.
Example 9-2IntList.java: a simple datatype that defines custom serialziation and de-serialization behavior for itself.
Example 10-1MultiLineLabel.java: a custom AWT component and Java Bean that displays a specified string of text, using multiple lines, if the string contains newline characters.
Example 10-2YesNoDialog.java: a bean that displays a dialog box.
Example 10-3AnswerEvent.java: an event type used by the bean.
Example 10-4AnswerListener.java: the event listener interface used by the bean
Example 10-5YesNoDialogBeanInfo.java: a BeanInfo class for the bean.
Example 10-6YesNoDialogAlignmentEditor.java: a property editor class for one of the bean's properties.
Example 10-7YesNoDialogMessageEditor.java: a property editor class for another of the bean's properties.
Example 10-8YesNoDialogCustomizer.java: a customizer class for the bean.
Example 11-1ConvertEncoding.java: an application that converts a file from one character encoding to another.
Example 11-3Portfolio.java: a dummy stock portfolio program that demonstrates internationalization of dates, times and numbers.
Example 11-4SimpleMenu.java: a convenience class for simple creation of localized menus using ResourceBundles.
Example 11-5Menus.properties Menus_en_GB.properties Menus_fr.properties: property files that specify default, British, and French resource bundles for simple menu creation.
Example 11-6LocalizedError.java: a class that displays a localized error message fora given exception object, using the MessageFormat class.
Example 11-7Errors.properties: a sample property file used by the previous example
Example 12-1ShowClass.java: a program that uses the Reflection API to show the fields and methods of a class.
Example 12-2UniversalActionListener.java: an ActionListener implementation that uses reflection to invoke a named method of an object.