Sun Java Solaris Communities My SDN Account Join SDN

Index of Code Samples

Examplets from Java Developer's Almanac

 

View PDF
Download PDF (1,273KB)

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.

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 GUI Creation (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

AWT Clipboard (java.awt.datatransfer)

Retrieving Text from the System Clipboard
Placing Text on the System Clipboard

AWT Drag-n-Drop (java.awt.dnd)

Making a Component Draggable
Making a Component a Drop Target
Handling a Drop Event

AWT 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

AWT 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

AWT Shapes (java.awt.geom)

Creating a Shape with Lines and Curves
Creating Basic Shapes
Combining Shapes
Scaling, Shearing, Translating, and Rotating a Shape

Compression -- Zips Files (java.util.zip)

Compressing a File
Decompressing a File
Listing the Contents of a ZIP File
Calculating the Checksum of a Byte Array

Compression -- JAR Files (java.util.jar)

Retrieving the Manifest of a JAR File

JDBC Technology -- Database Access (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

JavaBeans Technology (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

JNDI (javax.naming)

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

JNDI 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

JNDI Events (javax.naming.event)

Registering for Namespace Changes in the Directory
Registering for Object Changes in the Directory Ldap

JNDI LDAP (javax.naming.ldap)

Setting LDAP Connection Request Controls
Setting LDAP Context Request Controls
Getting LDAP Response Controls
Performing an LDAP ``Extended'' Operation

Language Essentials (java.lang)

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

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)

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
Starting the Name Server
Defining and Exporting a Portable Remote Object
Looking Up a Portable Remote Object and Invoking a Method

Sampled Sound (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

Sampled Sound -- MIDI (javax.sound.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 GUI Creation (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

Swing Event Handling (javax.swing.event)

Handling Hyperlink Events
Handling Changes to a List Component

Swing File Chooser (javax.swing.filechooser)

Adding a Filter to a File Chooser Dialog

Swing Text Component (javax.swing.text)

Inserting Styled Text in a Text Pane
Inserting an Image into a Text Pane
Inserting a Component into a Text Pane

Swing Tree Component (javax.swing.tree)

Creating a Tree
Handling Selection Events in a Tree Component

Swing Undo (javax.swing.undo)

Adding Undo and Redo to a Text Component

These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms Examplets provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.


Have a question about Java programming? Use Java Online Support.