Sun Java Solaris Communities My SDN Account Join SDN
 
Newsletters

Java Technology Fundamentals Newsletter

 
    October 31, 2003    

In this Issue

Welcome to the Java Developer Connection Java Technology Fundamentals Newsletter.

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 the JDC's New to Java Programming Center.

image Java Programming Language Basics: Properties Basics
image Java Bits: Lesson: Full-Screen Exclusive Mode API
image Making Sense of Java Classes: BorderFactory Class
image Program Challenge: Properties
image Sun Web Course: The Complete Java Technology Web Bundle
image For More Information: Read articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here.

.

Java Programming Language Basics

Properties Basics

Good programmers don't hard code configuration information. Instead, they store this information outside the realm of the source code so that changing system configuration doesn't require recompilation of the system. For the Java platform, those program settings are typically stored in properties files. While you could design and develop your own home grown solution, the java.util.Properties class offers an easy way to load and save these settings with little to no fuss in your code.

A properties file is a series of key-value pairs, stored in a file. The name of the file ends with .properties. For instance, the following two lines might represent a properties file with two properties. The first property of hello has a value of world and the second of goodbye has a value of cruel world.

    hello=world
    goodbye=cruel world

The Properties class basically offers a persistent hash table where all the keys and values are strings. Ignoring the loading part for the moment, accessing key-value pairs works similarly to working with a Hashtable, as Properties is a subclass of it:

    String value = (String)aPropertiesObject.get("hello");
    System.out.println("Value of hello is: " + value);

Notice the casting here to a String of results of get. Since the Properties class is only meant to work with strings, it offers special get and put methods: getProperty and setProperty. With getProperty, you can ask for the value of a specific property with getProperty(key) and also do the same but provide a default value in the event the key is not present with getProperty(key, default). Instead of returning an Object like the get method, getProperty will return a String object. The setProperty method works identically to the put method, but it requires both arguments to be strings: setProperty(keyString, valueString).

Loading of property settings is done through the mysteriously named load method. Just pass in an InputStream and the properties will be read in from the stream. Even though the stream is meant to contain lines of String objects, the load method accepts an InputStream, not a Reader object. The 8859_1 character encoding is hard-coded into the class definition and cannot be changed. You can however specify Unicode values by putting \uXXXX constants into the file.

The basic loading process goes as follows:

InputStream is = ...
Properties props = new Properties();
try {
  props.load(is);
} catch (IOException e) {
  System.err.println("Load failed");
}

The InputStream is typically a FileInputStream. However, if you've jarred up your web application, instead of using a FileInputStream, the InputStream would need to come from the JAR file. If that is the case, then you need to ask the class loader for that stream, as shown here:

InputStream is =
    this.getClass().getClassLoader().getResourceAsStream(
                                         "foo.properties");

This will look for the foo.properties file in the same directory as the class that is executing. The line says, get the Class object for the current class, get its class loader, and then finally get the properties file as a resource and provide it as a stream.

Once the properties file is loaded, you can then use the getProperty and putProperty methods.

To see what properties got loaded, use one of the list methods. This allows you to pass in an OutputStream or Writer object to write the properties to. By passing in System.out, the current settings are written to the console.

If you need to update the properties and resave them, the Properties class offers a store method. When called, you pass in an OutputStream and a String: store(OutputStream stream, String string). The string is stored as a comment in the first line of the file, allowing a file viewer to see what is supposed to be in the file. There is also a save method but it is deprecated because it didn't throw an IOException upon error saving.

The final bit of the Properties class is the propertyNames method which returns an Enumeration. This method will combine all of the current properties, including those loaded via the load method, with a default set of Properties passed into the Properties constructor. Thus, if you'd like to replicate the list behavior yourself, you should use the propertyNames method of Properties to enumerate through the elements, not the elements method inherited from Hashtable. With the elements method, you would only get the specific key-value pairs stored in the properties list, not a combined version with the defaults.

.
.

Java Bits

Lesson: Full-Screen Exclusive Mode API

Do you want to use high-performance graphics in the Java development environment? Have you always wanted to program a game, but your images wouldn't move fast enough? Has your slide show program not worked properly because you had no control over the user's display resolution? If you've been asking any of these questions, then the full-screen exclusive mode API may be what you're looking for.

This lesson covers an API introduced in the latest release of the Java 2 Platform Standard Edition, version 1.4.

More

.
.

Making Sense of the Java Class

Class BorderFactory

A border is the margin around a Swing component. Inserts supported in the AWT library are meant for a similar purpose, but Swing borders are more colorful and provide different types of styles. These objects are used for aesthetic purposes, and can be used to decorate labels, text fields, image objects, and other components.

You can create borders by calling the constructors of various border classes in the javax.swing.border package, but the BorderFactory class provides static methods for creating border objects. Using static methods require less memory by returning references to previously created and stored border objects.

To assign a specific border around a component, call the setBorder method:

public void setBorder(Border border);

For the Border parameters, call the BorderFactory class and the static method of the kind of border you want to create using the dot operator:

setBorder(BorderFactory.createRaisedBevelBorder());

For example, to create a button with a raised bevel border, use the following:

b1 = new JButton("Bevel Border");
b1.setBorder(BorderFactory.createRaisedBevelBorder());

The following application demonstrates how to make several different types of borders around Swing components:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderExamples extends JFrame {

   JButton b1, b2, b3, b4;
   JLabel b5;
   JPanel p;
   
   public BorderExamples () {
    
      b1 = new JButton("Bevel Border");
      b1.setBorder(BorderFactory.createRaisedBevelBorder());
      b2 = new JButton("Etched Border");
      b2.setBorder(BorderFactory.createEtchedBorder());
      b3 = new JButton("Line Border");
      b3.setBorder(BorderFactory.createLineBorder(Color.RED));
      b4 = new JButton("Lowered Bevel Border");
      b4.setBorder(BorderFactory.createLoweredBevelBorder());
      b5 = new JLabel("");
      b5.setBorder(BorderFactory.createTitledBorder("Titled Label"));
                   
      p = new JPanel();
      p.setLayout(new GridLayout(3,2));
      p.add(b1);
      p.add(b2);
      p.add(b3);
      p.add(b4);
      p.add(b5);
      
      getContentPane().add(p,BorderLayout.CENTER);
      setBounds(300, 300, 500, 500);
      pack();
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
      
      public static void main (String args[]) {
        BorderExamples be = new BorderExamples();
        }
      }

.
.

Program Challenge

Properties

  1. Create a program that loads a set of properties from a file name specified at the command line.
  2. Offer a default set of property settings. Make sure the property file defines at least one property named first.
  3. Have the program display the value of the property first, then have the program display all the property values, both by going through all the elements yourself and by using the list method of Properties.

See a possible solution to the Challenge.

.
.
.

Sun Web Course

The Complete Java Technology Web Bundle

The Complete Java Technology Web Bundle provides you with access to all of Sun's Java technology web-based courses. This series describes a the Java programming language, object-oriented analysis and design, distributed computing with Java technologies, servlets, and Enterprise JavaBeans technology. More specialized technologies include wireless applications using the Java 2 Platform, Micro Edition (J2ME) and the Mobile Information Device (MID) Profile, Java Card technology, and Jini technology.

This bundle also allows you to access any new Java technology courses that become available during your subscription period.

.
.

For More Information

.
.

Downloading the Java 2 Platform

For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit.

.
.

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.
- Wireless 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


Comments? Send your feedback on the Java Technology Fundamentals Newsletter to: dana.nourie@sun.com

Go to the subscriptions page to subscribe or unsubscribe to this newsletter.

ARCHIVES: You'll find the Java Technology Fundamentals Newsletter archives at:
http://developer.java.sun.com/developer/onlineTraining/new2java/supplements/


Copyright 2003 Sun Microsystems, Inc. All rights reserved. 4150 Network Circle, Santa Clara, CA 95054

Trademark Information: http://www.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.