Java Examples in a Nutshell

Java Examples in a Nutshell

5.0 1
by David Flanagan

View All Available Formats & Editions

The author of the best-selling Java in a Nutshell has created an entire book of real-world Java programming examples that you can learn from. If you learn best "by example," this is the book for you.This third edition covers Java 1.4 and contains 193 complete, practical examples: over 21,900 lines of densely commented, professionally written Java code,

…  See more details below


The author of the best-selling Java in a Nutshell has created an entire book of real-world Java programming examples that you can learn from. If you learn best "by example," this is the book for you.This third edition covers Java 1.4 and contains 193 complete, practical examples: over 21,900 lines of densely commented, professionally written Java code, covering 20 distinct client-side and server-side APIs. It includes new chapters on the Java Sound API and the New I/O API. The chapters on XML and servlets have been rewritten to cover the latest versions of the specifications and to demonstrate best practices for Java 1.4. New and updated examples throughout the book demonstrate many other new Java features and APIs.Java Examples in a Nutshell is a companion volume to Java in a Nutshell, Java Foundation Classes in a Nutshell, and Java Enterprise in a Nutshell. It picks up where those quick references leave off, providing a wealth of examples for both novices and experts. This book doesn't hold your hand; it simply delivers well-commented working examples with succinct explanations to help you learn and explore Java and its APIs.Java Examples in a Nutshell contains examples that demonstrate:

  • Core APIs, including I/O, New I/O, threads, networking, security, serialization, and reflection
  • Desktop APIs, highlighting Swing GUIs, Java 2D graphics, preferences, printing, drag-and-drop, JavaBeans, applets, and sound
  • Enterprise APIs, including JDBC (database access), JAXP (XML parsing and transformation), Servlets 2.4, JSP 2.0 (JavaServer Pages), and RMI
The book begins with introductory examples demonstrating structured and object-oriented programming techniques for new Java programmers. A special index at the end of the book makes it easy to look up examples that use a particular Java class or accomplish a desired task. In between, each chapter includes exercises that challenge readers and suggest further avenues for exploration.

Read More

Editorial Reviews

Fatbrain Review

Designed to accompany Java in a Nutshell, this companion tutorial provides practical, real-world Java programming examples. Intended for novice and advanced Java programmers, the book is grouped by skill level.

Like his previous Java books, author David Flanagan maintains his cut-to-the-chase approach. He teaches by example, and he doesn't include detailed documentation of the exact syntax and behavior of Java statements. Instead, he includes examples from the first and the second (Java 1.1) editions of Java in a Nutshell. He also covers the new AWT event handling model, the JavaBeans API, internationalization, reflection and object serialization. You will find new programming examples for RMI, database connectivity and security. There are also practical examples for I/O, networking and GUIs. The appendix contains Swing, a set of new, cross-platform, lightweight components that form part of the forthcoming Java Foundation Classes (JFC). The end-of-the-chapter exercises provide further avenues for exploring the Java programming.

Product Details

O'Reilly Media, Incorporated
Publication date:
In a Nutshell (O'Reilly) Series
Edition description:
Third Edition
Product dimensions:
5.92(w) x 8.94(h) x 1.24(d)

Read an Excerpt

Chapter 11: Java Beans

Defining a Simple Property Editor
A bean can also provide an auxiliary Property Editor for use by a beanbox tool. PropertyEditor is a flexible interface that allows a bean to tell a beanbox how to display and edit the values of certain types of properties.

A beanbox tool always provides simple property editors for common property types such as strings, numbers, fonts, and colors. If your bean has properties of a non-standard type, however, you should register a property editor for that type. The easiest way to "register" a property, editor is through a simple naming convention. If your type is defined by the class X, the editor for it should be defined in the class XEditor. Alternatively, you can register a property editor by calling the PropertyEditorManager.registerEditor( ) method, probably from the constructor of your BeanInfo class. If you call this method from the bean itself, the bean then depends on the property editor class, so the editor has to be bundled with the bean in applications, which is not desirable.

In our YesNoDialog example, we don't define any new data types, but we still have two individual properties that need custom editors. In this case, we register the property editors for individual properties by specifying them in the Property-Descriptor objects returned by the getPropertyDescriptors( ) method of our BeanInfo class.

The PropertyEditor interface can seem confusing at first. Its methods allow you to define three techniques for displaying the value of a property and two techniques for allowing the user to edit the value of a property. The value of a property can he displayed:

  • As a string. If you define the qetAsText( ) method, a beanbox can convert a property to a string and display that string to the user.
  • As an enumerated value. If a property can only take on values from a fixed set of values, you can define the getTags( ) method to allow a beanbox to display a dropdown menu of allowed values for the property.
  • In a graphical display. If you define paintValue( ), a beanbox can ask the property editor to display the value using some natural graphical format, such as a color swatch for colors. You also need to define isPaintable( ) to specify that a graphical format is supported.
The two editing techniques are:
  • String editing If you define the setAsText( ) method, a beanbox knows it can simply have the user type a value into a text field and pass that value to setAsText( ). If your property editor defines getTags( ), it should also define setAsText( ) so that a beanbox can set the property value using the individual tag values.
  • Custom editing if your property editor defines getCustomEditor( ), a bean-box can call it to obtain some kind of AWT component that can be displayed in a dialog box and serve as a custom editor for the property. You also need to define supportsCustomEditor( ) to specify that custom editing is supported.
The setValue( ) method of a PropertyEditor is called to specify the current value of the property. It is this value that should be converted to a string or graphical representation by getAsText( ) or paintValue( ).

A property editor must maintain a list of event listeners that are interested in changes to the value of the property. The addPropertyChangeListener( ) and removePropertyChangeListener( ) methods are standard event listener registration and removal methods. When a property editor changes the value of a property, either through setAsText( ) or through a custom editor, it must send a PropertychangeEvent to all registered listeners.

PropertyEditor defines the getJavaInitializationString( ) for use by beanbox tools that generate Java code. This method should return a fragment of Java code that can be used to initialize variable to the current property value.

Finally, a class that implements the PropertyEditor interface must have a no-argument constructor, So that it can be dynamically loaded and instantiated by a beanbox.

Most property editors can be much simpler than this detailed description would suggest. In many cases, you can subclass PropertyEditorSupport instead of implementing the PropertyEditor! interface directly. This useful class provides no-op implementations of most PropertyEditor methods. It also implements the methods for adding and removing event listeners.

A property that has an enumerated value requires a simple property editor. The alignment property of the YesNoDialog bean is an example of this common type of property. The property is defined as an int, but it has only three legal values, defined by the constants LEFT, CENTER, and RIGHT. By default, a beanbox only knows that the property is an int, so it displays the property as a number and allows the user to enter any integer as a property value. Instead, we would like the beanbox to display one of the strings "left." "center" or "right" as the value, and allow the user to choose from these values when setting the property. This can be done with getTags( ) and setAsText( ) methods of a property editor, as shown in Example 11-6.

This example creates the YesNoDialogAlignmentEditor class, which is registered as a PropertyEditor for the alignnment property by the YesNoDialogBeanInfo class shown in Example 11-5. The property editor subclasses PropertyEditorSupport, so it is relatively short. Notice that it passes Integer objects in the calls to set-Value( ) that are made from the setAsText( ) method. You need to use wrapper objects for any primitive-type properties. The use of the Integer class is also apparent in the definition of getJavaInitializationString( ). The setValue( ) method of PropertyEditorSupport handles notifying registered PropertyChange-Listener objects about changes in the value of the property, so this simple property editor does not need to be aware of the existence of such listeners.

Read More

Meet the Author

David Flanagan is a computer programmer who spends most of his time writing about JavaScript and Java. His books with O'Reilly include Java in a Nutshell, Java Examples in a Nutshell, Java Foundation Classes in a Nutshell, JavaScript: The Definitive Guide, and JavaScript Pocket Reference. David has a degree in computer science and engineering from the Massachusetts Institute of Technology. He lives with his wife and son in the U.S. Pacific Northwest bewteen the cities of Seattle, Washington and Vancouver, British Columbia. David has a simple website at

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >