You are receiving this e-mail {e-mail address} because you elected to receive e-mail from Sun Microsystems, Inc. To update your communications preferences, please see the link at the bottom of this message. We respect your privacy and post our privacy policy prominently on our Web site http://sun.com/privacy/
  Welcome to the Java Technology Fundamentals Newsletter.
Java Technology Fundamentals
NEWSLETTER
July 2005
  This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to Sun Developer Network's New to Java Programming Center.

You can receive future issues of this newsletter in HTML or text:
http://developer.java.sun.com/subscription/

Note: For the code to compile in this issue of Fundamentals, you need to use the JDK 5.0 software.
http://java.sun.com/j2se/1.5.0/download.jsp

Contents
 
» Java Programming Language Basics: Displaying Multiline Messages in JOptionPanes
» Making Sense of the Java Classes & Tools: New Java 2D Features in J2SE 5.0 Lesson: Regular Expressions
» Java Bits: J2SE SDK/JRE Version String Naming Convention
» Using Assertions in Java Technology
» What's New on java.net: Pixel Pushing
» Sun's Java Technology Instructor-Led Courses: Java Sun CAP for OO Programmers Preparing to Become a Sun Certified Programmer (PK-CERT-JA1B)
» Sun's Java Technology Exams: Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (CX-310-055)
» Books for Learning: The Java Language Specification
» For More Information

Java Programming Language Basics
 
Displaying Multiline Messages in JOptionPanes


The JOptionPane component is typically used to show short messages or to prompt a user for input. If, however, you need to show a longer message, the component doesn't support word wrap by default. You can certainly add "\n" characters to the text message, but if you don't want to count characters and width, you need to do a little extra work to have the component wrap the text for you.

Word wrap is controlled by the maxCharactersPerLineCount property of JOptionPane. The maxCharactersPerLineCount property is set to an extremely large value, Integer.MAX_VALUE, by default. If you want to change the setting, you must subclass JOptionPane and override the public int getMaxCharactersPerLineCount() method. This causes a long text message to be broken up into multiple lines for you within an option pane. In addition, you can't use any of the static methods because they don't know about your custom subclass.

To help you create narrow JOptionPane components, you can use the following method in your class. The new method provides a way of specifying the desired option pane character width.
public static JOptionPane getNarrowOptionPane(
int maxCharactersPerLineCount) { 
    // Our inner class definition
    class NarrowOptionPane extends JOptionPane { 
      int maxCharactersPerLineCount;
      NarrowOptionPane(int maxCharactersPerLineCount) { 
        this.maxCharactersPerLineCount = maxCharactersPerLineCount;
      } 
      public int getMaxCharactersPerLineCount() { 
        return maxCharactersPerLineCount;
      } 
    } 
    return new NarrowOptionPane(maxCharactersPerLineCount);
  }
Once the method is defined, you can create an option pane of controlled character width, manually configure all the properties, place it in a pop-up window, show it, and then determine the user's response. If the message of the pane is long, the text will wrap. The following source demonstrates usage of these new capabilities, with the long message trimmed a bit. Imagine the text repeated five or six times.
  String msg =
    "this is a really long message ... this is a really long message";
  JOptionPane optionPane = getNarrowOptionPane(72);
  optionPane.setMessage(msg);
  optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
  JDialog dialog = optionPane.createDialog(source, "Width 72");
  dialog.setVisible(true);

First, here's what the screen looks like if you didn't change the maxCharactersPerLineCount property.

Wide JOptionPane
Click image for a larger view.

Next, here's the now narrow JOptionPane:

Narrow JOptionPane

Although this seems like a lot of work, it's the best way of creating multiline option panes, unless you want to manually parse the message into separate lines.

Making Sense of the Java Classes & Tools
 
New Java 2D Features in J2SE 5.0


Caching All BufferedImages

As of J2SE 5.0, all images created with a BufferedImage constructor are managed images and can be cached in video memory or, in the case of a remote X server, on the X server side. Previously, the Sun implementation managed only compatible images -- those created with the Component createImage(int, int)method or with the GraphicsConfiguration createCompatibleImage methods. Managed images generally perform better than unmanaged images.

Methods for Controlling Hardware Acceleration of Images

The bug report that corresponds to this change is 4881082.

The Image class has three new methods related to hardware acceleration. The getCapabilities method, formerly defined only in VolatileImage, allows you to determine whether the image is currently accelerated. Two other methods let you set or get a hint about how important acceleration is for the image: setAccelerationPriority and getAccelerationPriority.

The GraphicsConfiguration class has two new methods, createCompatibleVolatileImage(int, int, int) and createCompatibleVolatileImage(int, int, ImageCapabilities, int), that allow you to create transparent VolatileImages.

Note: In J2SE 5.0, these methods are not fully operational. The value set by setAccelerationPriority is ignored, and images created with the createCompatibleVolatileImage methods are not always hardware accelerated. On Linux and Solaris systems, only OPAQUE VolatileImages are hardware accelerated. On Microsoft Windows systems, images created with createCompatibleVolatileImage in J2SE 5.0 are hardware accelerated only if the hardware supports acceleration and one of the following is true:

  • The transparency value is OPAQUE.
  • The transparency value is TRANSLUCENT and translucency acceleration has been specifically enabled at runtime (sun.java2d.translaccel=true).


  • Read more

    Lesson: Regular Expressions

    What are regular expressions?

    Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set. They can be used as a tool to search, edit, or manipulate text or data. You must learn a specific syntax to create regular expressions -- one that goes beyond the normal syntax of the Java programming language. Regular expressions range from being simple to quite complex, but once you understand the basics of how they're constructed, you'll be able to understand any regular expression.

    This tutorial will teach you the regular expression syntax supported by the java.util.regex API (in the API reference documentation), and it will present plenty of working examples to illustrate how the various objects interact. In the world of regular expressions, there are many different flavors to choose from, such as grep, Perl, Tcl, Python, PHP, and awk. The regular expressions in the java.util.regex API are most similar to Perl.

    How are regular expressions represented in this package?

    The java.util.regex package consists of three classes, all in the API reference documentation: Pattern, Matcher, and PatternSyntaxException.

  • A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must call one of its public static compile methods, both of which will return a Pattern object. You should understand regular expressions fairly well to use these methods, since they accept a regular expression (a string) as their first argument. The first few lessons of this tutorial will teach you the required syntax.
  • A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by calling the public matcher method on a Pattern object.
  • A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.


  • In the last few sections in this lesson, we'll explore each class in detail with a separate section devoted to each class. But first, let's learn the details of how regular expressions are created. The next section introduces a simple test harness that we'll use repeatedly to explore how regular expressions are constructed.

    Read more

    Java Bits
     
    J2SE SDK/JRE Version String Naming Convention


    Developers may need to know a product's version string for various purposes, depending on whether they are developing and deploying a product or downloading and integrating a product.

    The following table explains how to interpret SDK/JRE release version string information.

    Note that this convention was not in effect prior to the 1.3.0 feature release. The output of java -version has had the same format since 1.3.1 system properties and the java -version command.

    The output of the java -version command includes a product version identifier and a build identifier. This output is determined by the values of several system properties, and those system properties can themselves be examined programmatically at runtime.

    Read more

    Using Assertions in Java Technology by Qusay H. Mahmoud
     
    Finding bugs in programs is not an easy task, especially when they are subtle bugs. The process of finding bugs is not exciting, and you may have gone through your program wasting your time trying to find bugs that should not have existed in the first place. Such bugs may exist because you did not understand the specifications correctly. And as Barry Boehm, the father of software economics, put it: If it costs $1 to find and fix a requirement-based problem during the requirements definition process, it can cost $5 to repair it during design, $10 during coding, $20 during unit testing, and as much as $200 after delivery of the system. Thus, finding bugs early on in the software life cycle pays off.

    You can use assertions to detect errors that may otherwise go unnoticed. Assertions contain Boolean expressions that define the correct state of your program at specific points in the program source code. The designers of the Java platform, however, didn't include support for assertions. Perhaps they viewed exceptions as a superior feature, allowing you to use try/catch/finally to throw an exception instead of aborting the program as in assertions. But the Java 2 Platform, Standard Edition (J2SE) release 1.4, has introduced a built-in assertion facility. This article does the following:
    • Presents an overview of design by contract
    • Presents an overview of assertions
    • Shows how to roll your own assertion capabilities
    • Describes the new assertion facility
    • Shows how to use the new assertion facility
    • Offers guidelines for using assertions
    • Presents examples of how to use assertions
    Read more

    What's New on java.net
     
    Pixel Pushing by Jonathan Simon


    Building a graphical interface is hyperdetailed, laborious work. A server-side or nongraphical component can usually get by by fixing an interface that needs a little reworking "in the next release."

    Graphical interfaces simply don't have that option. This is because any detail left unattended is displayed in plain sight to the users of our applications. Adjacent borders, buttons, and text have to line up to the pixel -- otherwise, the interface simply looks sloppy. And we, as graphical interface developers, need to strive to make interfaces that don't look sloppy.

    With that in mind, we are going to focus on the nitty-gritty, pixel- level details of a graphical interface. In this article, we'll copy a native operating system icon in Java technology code and explore some tools and processes that can help you get used to working in the pixel-pushing world.

    Read the rest of this article

    Sun's Java Technology Instructor-Led Courses
     
    Java Sun CAP for OO Programmers Preparing to Become a Sun Certified Programmer (PK-CERT-JA1B)


    The PK-CERT-JA1B CAP is for Object-Oriented programmers preparing to become a Sun Certified Programmer for the Java 2 Platform, Standard Edition.

    Learn more about this course

    Sun's Java Technology Exams
     
    Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (CX-310-055)


    The Sun Certified Programmer for Java 2 Platform 5.0 certification exam is for programmers experienced in using the Java programming language. Achieving this certification provides clear evidence that a programmer understands the basic syntax and structure of the Java programming language and can create Java technology applications that run on server and desktop systems using J2SE 5.0.

    Learn more

    Books for Learning
     
    The Java Language Specification


    Written by the inventors of the technology, The "Java Language Specification, Third Edition," is the definitive technical reference for the Java programming language. If you want to know the precise meaning of the language's constructs, this is the source for you. The book provides complete, accurate, and detailed coverage of the Java programming language. It provides full coverage of all new features added since the previous edition, including generics, annotations, asserts, autoboxing, enums, for each loops, and static import clauses.

    More information

    For More Information
     
    The terms "Java Virtual Machine" and "JVM" mean a virtual machine for the Java platform.


    Downloading the Java 2 Platform, Standard Edition (J2SE)
     
    For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit.

    Rate and Review
    Tell us what you think of the content of this page.
    Excellent   Good   Fair   Poor  
    Comments:
    If you would like a reply to your comment, please submit your email address:
    Note: We may not respond to all submitted comments.
    Comments? Send your feedback on the Java Technology Fundamentals Newsletter to: fundamentals_newsletter@sun.com

    Find archived issues of the following Java technology developer newsletters or manage your current newsletter subscriptions: https://softwarereg.sun.com/registration/developer/en_US/subscriptions

    Subscribe to the following newsletters for the latest information about technologies and products in other Java platforms:
    • Core Java Technologies Newsletter. Learn about new products, tools, resources, and events of interest to developers working with core Java technologies.
    • Mobility Developer Newsletter. Learn about the latest releases, tools, and resources for developers working on wireless and Java Card technologies and applications.
    • Core Java Technologies Tech Tips (formerly JDC Tech Tips) Get expert tips, sample code solutions, and techniques for developing in the Java 2 Platform, Standard Edition (J2SE)
    You can subscribe to these and other JDC publications on the JDC Newsletters and Publications page

    IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies:
    http://www.sun.com/share/text/termsofuse.html
    http://www.sun.com/privacy/
    http://developer.java.sun.com/berkeley_license.html

    © 2005 Sun Microsystems, Inc. All Rights Reserved. For information on Sun's trademarks see: http://sun.com/suntrademarks
    Java, J2EE, J2SE, J2ME, and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

    To update your Sun subscription preferences or unsubscribe to Sun news services, click here or use this link https://softwarereg.sun.com/registration/developer/en_US/subscriptions

    Sun Microsystems, Inc. 10 Network Circle, MPK10-209 Menlo Park, CA 94025 US