The following code ExampletsTM
are Copyrighted 2000 by Addison-Wesley and Patrick Chan
and are provided as a courtesy for your educational, non-commercial use.
Examplets are code snippets that illustrate a particular task.
They're designed to show which classes and members are involved in
performing the task, and how the classes and members interact with
each other. You will see blue italicized text in an examplet; this
indicates code that should be replaced if you use it in your
program.
An examplet
can have enough detail for you to use it, as is. Or you might have
to get more information about the involved classes. A good place to
find information about the Java 2 Platform APIs is the Java
2 Platform API index.
It's also
good practice to add catch clauses, as appropriate, to the
examplets. If the code of an examplet throws a checked exception,
the code is surrounded by a try/catch statement. However, to reduce
space and distraction, the catch clause is left blank. Catch
clauses should always handle the exception and should never be left
blank. So, at the very least, you should include a call to
e.printStackTrace() to display the exception on the
console.
You'll find
examplets in a variety of categories. See the Table of Contents for a list of categories, and the
examplets in each category.
Table of
Contents
Accessibility
(javax.accessibility) |
 |
The Quintessential Accessible
Object
Setting an Accessible Name
for an Image Button
Setting a Description for
Image Icons
Setting a Mnemomic for
Buttons
Setting a Mnemonic for a
Menu
Associating a Label with a
Component
Setting a Keyboard
Accelerator for a Menu Item
|
Applets (java.applet) |
 |
The Quintessential Applet
Getting an Applet Parameter
Making the Browser Visit a
URL
Showing a Message in the Browser's
Status Bar
Loading and Playing Audio in an
Applet
Loading and Playing Audio in an
Application
Loading and Painting an Image in an
Applet
Animating an Array of Images in an
Applet
|
AWT (Abstract Windowing
Toolkit) (java.awt) |
 |
The Quintessential Drawing Program
Drawing Basic Shapes
Filling Basic Shapes
Loading an Image
Drawing an Image
Scaling, Shearing, Translating, and
Rotating a Drawn Image
Drawing Simple Text
Drawing Rotated Text
Getting the Dimensions of Text
Drawing Anti-Aliased Text and
Graphics
Drawing with Alpha
Setting the Clipping Area with a
Shape
Changing the Thickness of the Stroking
Pen
Creating a Shape from a Stroked
Shape
Stroking or Filling a Shape
Stroking or Filling with a
Color
Stroking or Filling with a Gradient
Color
Stroking or Filling with a
Texture
Animating an Array of Images in an
Application
Setting a Component's Cursor
|
 |
Clipboard
(java.awt.datatransfer) |
 |
 |
Retrieving Text from the System
Clipboard
Placing Text on the System
Clipboard
|
 |
Drag and
Drop (java.awt.dnd) |
 |
 |
Making a Component Draggable
Making a Component a Drop
Target
Handling a Drop Event
|
 |
Events
(java.awt.event) |
 |
 |
Handling Events with an Anonymous
Class
Handling Action Events
Handling Key Presses
Handling Mouse Clicks
Handling Mouse Motion
Detecting Double and Triple
Clicks
Handling Focus Changes
|
 |
Fonts
(java.awt.font) |
 |
 |
Listing All Available Fonts
Drawing a Paragraph of
Text
Getting the Shape from the Outline
of Text
Drawing Text with Mixed
Styles
|
 |
Shapes
(java.awt.geom) |
 |
 |
Creating a Shape with Lines and
Curves
Creating Basic Shapes
Combining Shapes
Scaling, Shearing, Translating,
and Rotating a Shape
|
Compression (java.util.zip) |
 |
Compressing a File
Decompressing a File
Listing the Contents of a ZIP
File
Calculating the Checksum of a
Byte Array
|
 |
JAR files
(java.util.jar) |
 |
 |
Retrieving the Manifest of a JAR
File
|
Database - JDBC (java.sql) |
 |
Connecting to a Database
Creating a Database Table
Entering a New Row into a Database
Table
Getting All Rows from a Database
Table
Getting Particular Rows from a
Database Table
Updating a Row of Data in a Database
Table
Using a Prepared SQL Statement
|
Data Structures and
Algorithms (java.util) |
 |
Creating a Set
Creating a List
Creating a Hash Table
Truncating a List
Converting a Collection to an
Array
Converting an Array to a
Collection
Implementing a Queue
Implementing a Stack
Iterating the Elements of a
Collection
Sorting an Array
Finding an Element in a Sorted
Array
Finding an Element in a Sorted
List
Inserting an Element into a Sorted
List
Generating a Random Number
Breaking a String into Words
Reading Properties from a Properties
File
Creating Date Object From a
Particular Date
Parsing a Date Using a
Pattern
Reading and Writing a Properties
File
Getting and Setting
Properties
Scheduling a Timer Task to Run at a
Certain Time
Scheduling a Timer Task to Run
Repeatedly
|
Files, Streams, I/O
(java.io) |
 |
Constructing a Path
Reading Text from Standard
Input
Reading Text from a File
Writing to a File
Creating a Directory
Appending to a File
Deleting a File
Deleting a Directory
Creating a Temporary File
Using a Random Access File
Listing the File System Roots
Setting File Modification Time
Serializing an Object
Deserializing an Object
Traversing a Directory
Reading UTF-8 Encoded Data
Writing UTF-8 Encoded Data
Reading ISO Latin-1 Encoded
Data
Writing ISO Latin-1 Encoded
Data
|
Images (java.awt.image) |
 |
Converting an Image to a Buffered
Image
Getting Pixels from a Buffered
Image
Getting a Sub-Image of an
Image
Creating and Drawing on a
Buffered Image
Scaling, Shearing, Translating,
and Rotating an Image
Converting a Color Image to
Gray
Blurring an Image
Sharpening an Image
Embossing an Image
|
Internationization
(java.text) |
 |
Formatting and Parsing a Locale-specific
Number
Formatting and Parsing
Locale-specific Currency
Formatting and Parsing a
Locale-specific Percentage
Formatting and Parsing a
Locale-specific Date
Formatting and Parsing
Locale-specific Time
Formatting and Parsing a
Locale-specific Date and Time
Determining the Type of a
Character
Comparing Strings in a
Locale-independent Way
Determining the Character Boundaries
in a Unicode String
Determining the Word Boundaries in a
Unicode String
Determining the Sentence Boundaries
in a Unicode String
Determining Potential Line Breaks in
a Unicode String
|
JavaBeans TM (java.beans) |
 |
The Quintessential Bean
Implementing a Bound Property
Implementing a Constrained
Property
Instantiating a Bean
Handling a Property Change
Event
Handling a Vetoable Property Change
Event
|
Math (java.math) |
 |
Operating With Big Integer Values
Operating With Big Decimal
Values
Setting the Decimal Place of a Big
Decimal Value
|
Networking (java.net) |
 |
Creating a URL
Parsing a URL
Reading Text from a URL
Calling a CGI Using POST
Method
Resolving a Hostname
Creating a Client Socket
Creating a Server Socket
Reading Text from a Socket
Writing Text to a Socket
Sending a Datagram
Receiving a Datagram
Joining a Multicast Group
Receiving from a Multicast
Group
Sending to a Multicast Group
|
Printing (java.awt.print) |
 |
The Quintessential Printing
Program
Getting the Dimensions of a
Printed Page
Setting the Orientation of a
Printed Page
Printing Pages with Different
Formats
Displaying the Page Format
Dialog
Displaying the Print
Dialog
|
Reference Objects
(java.lang.ref) |
 |
Holding onto an Object Until Memory
Becomes Low
Determining When an Object Is No
Longer Used
Determining When an Object Is
About to be Reclaimed
|
Reflection (java.lang.reflect) |
 |
Getting a Class Object
Getting the Modifiers of a
Class Object
Getting the Interfaces of a
Class Object
Getting the Methods of a
Class Object
Getting a Constructor of a
Class Object
Creating an Object Using a
Constructor Object
Getting the Field Objects of
a Class Object
Getting and Setting the Value
of a Field
Invoking a Method Using a
Method Object
Getting the Modifiers of a
Member Object
Getting the Name of a Class
or Member Object
Determining if an Object Is
an Array
Determining the Dimensions of
an Array
Getting the Component Type of
an Array
Creating an Array
Expanding an Array
Getting and Setting the Value
of an Element in an Array
Overriding Default
Access
Creating a Proxy
Object
|
RMI (java.rmi) |
 |
Starting Up the RMI Registry
Defining and Exporting a Remote
Object
Looking Up a Remote Object and
Invoking a Method
Passing Parameters to a Remote
Method
Returning Values from a Remote
Method
Throwing an Exception from a Remote
Method
|
Sound |
 |
Sampled
(javax.sound.sampled) |
 |
 |
Loading and Playing Sampled
Audio
Determining the File Format
of a Sampled Audio File
Determining the Encoding of
a Sampled Audio File
Determining the Duration of
a Sampled Audio File
Playing Streaming Sampled
Audio
Continuously Playing a
Sampled Audio File
Determining the Position of
a Sampled Audio Player
Determining When a Sampled
Audio Player has Finished Playing
Controlling the Volume of
Playing Sampled Audio
|
 |
Midi |
 |
 |
Loading and Playing Midi Audio
Determining the File Format of
a MIdi Audio File
Determining the Duration of a
Midi Audio File
Playing Streaming Midi
Audio
Determining the Position of a
Midi Sequencer
Determining When a Midi Audio
Player has Finished Playing
Controlling the Volume of
Playing Midi Audio
|
Security (java.security) |
 |
Creating a New Key Pair and Self-signed
Certificate
Creating and Signing a JAR
File
Creating a New Policy
File
Managing Policy Files
Creating a Keyed Digest Using
MD5
Getting a Certificate from a
Keystore
Checking Read/Write Permission
for a Directory
Checking if One Permission
Implies Another
Creating a Secure Random
Number
|
Swing (javax.swing) |
 |
The Quintessential Swing User
Interface
Centering a Frame on the
Screen
Exiting an Application When a Frame
Is Closed
Creating a Button
Creating a Checkbox Button
Creating a Color Chooser
Dialog
Creating a ComboBox
Creating Image Radio
Buttons
Retrieving the Selected Button in a
Group
Firing Item Events
Creating a File Chooser
Dialog
Displaying Only Directories in a
File Chooser Dialog
Creating an Internal Frame
Creating a List Component
Creating a Scrolling List
Component
Setting the Selection Mode of a
List Component
Creating a Menu Bar, Menu, and Menu
Item
Separating Menu Items in a
Menu
Creating a Popup Menu
Creating a Progess Bar
Creating Radio Buttons
Creating a Scroll Pane
Creating a Slider
Creating a Split Pane
Creating a Tabbed Pane
Creating a Text Field
Creating a Password Text
Field
Creating a Table
Creating a Text Area
Displaying Simple HTML
Files
Creating a Toolbar
Creating a Borderless
Window
Showing a Dialog Box
Creating Key Strokes and Binding
Them to Actions
Adding an InputMap to a
Component
Setting a Tool Tip
Laying Out Components in a Row or
Column
Separating Components in a Row or
Column
Laying Out Components in a Flow
(Left-to-Right, Top-to-Bottom)
Laying Out Components in a
Grid
Laying Out Components Using
Absolute Coordinates
Setting a Border
Adding a Title to a Border
Creating a Compound Border
Determining the Available Look and
Feels
Setting the Look and Feel
Painting the Background of a
Container with an Image Pattern
|
 |
Events
(javax.swing.event) |
 |
 |
Handling Hyperlink Events
Handling Changes to a List
Component
|
 |
File Chooser
(javax.swing.filechooser) |
 |
 |
Adding a Filter to a File
Chooser Dialog
|
 |
Tree
(javax.swing.tree) |
 |
 |
Creating a Tree
Handling Selection Events in a
Tree Component
|
 |
Text (javax.swing.text) |
 |
 |
Inserting Styled Text in a Text
Pane
Inserting an Image into a Text
Pane
Inserting a Component into a
Text Pane
|
java.lang |
 |
The Quintessential Java Application
Exiting a Java Program
The Quintessential Thread
Making an Object Cloneable
Constructing a String
Getting a Substring from a
String
Searching a String
Replacing Characters in a
String
Replacing Substrings in a
String
Converting a String to Upper or Lower
Case
Converting a String to a
Number
Shifting Elements in an Array
Copying Elements from One Array to
Another
Retrieving the SuperClass of an
Object
Getting the Package of an
Object
Implementing a Work Queue
Executes a Command
Reading Output from a Command
Sending Input to a Command
Pausing
Getting the Current Time
Getting the Value of a System
Property
Setting the Value of a System
Property
Using a Thread-local Variable
Loading Native Code
Converting Unicode to UTF-8
Converting UTF-8 to Unicode
Determining a Character's Unicode
Block
|
JNDI |
 |
Creating an Initial Context to the Naming
Service
Looking Up an Object From the
Naming Service
Listing a Context in the Naming
Service
Adding, Replacing, Removing, and
Renaming a Binding in the Naming Service
Creating and Destroying a
Subcontext in the Naming Service
Getting an Object's Fully
Qualified Name
Using a URL as a Name to the
Initial Context
Parsing a Composite Name
Parsing a Compound Name
|
 |
Directory
(javax.naming.directory) |
 |
 |
Creating an Initial Context to a
Directory
Reading an Object's
Attributes from the Directory
Modifying an Object's
Attributes in the Directory
Creating an Directory
Entry
Adding a Binding with
Attributes to the Directory
Performing a Basic
Directory Search
Searching the Directory
By Using a Search Filter
Searching a Subtree in
the Directory
Cancelling a Directory
Search
Getting an Object's
Schema from the Directory
Getting an Attribute's
Schema from the Directory
Authenticating to the
Directory
|
 |
Event (javax.naming.event) |
 |
 |
Registering for Namespace Changes in
the Directory
Registering for Object
Changes in the Directory
|
 |
Ldap (javax.naming.ldap) |
 |
 |
Setting LDAP Connection Request
Controls
Setting LDAP Context Request
Controls
Getting LDAP Response
Controls
Performing an LDAP
``Extended'' Operation
|
javax.rmi |
 |
Starting the Name Server
Defining and Exporting a Portable
Remote Object
Looking Up a Portable Remote Object
and Invoking a Method
|
javax.swing.undo |
 |
Adding Undo and Redo to a Text
Component
|
|