Developing Java Applications: A Tutorial | Creating a Custom View Class

Implementing the Custom View

Generate the .java File

The classes under NSObject in the Classes display of the nib file window represent, in most cases, both Java and Objective-C versions of the same class. When you create source code files from your nib-file definitions, you must specify which language you want.

  1. Select TempImageView in the Classes display of the nib file window.
  2. Select the Attributes display of the inspector.
  3. Click the Java radio button.



  4. Choose Create Files from the Classes menu.
  5. Respond Yes to both confirmation prompts.

Implement the Constructor

The constructor for TempImageView loads image files from the application's resources, converts them to NSImage objects, and assigns these to instance variables. (You'll add these images to the project later in this tutorial.) It also sets certain inherited attributes of the image view. The following procedure approaches the implementation of this constructor in three steps.

  1. Open TempImageView.java in Project Builder's project browser by clicking the filename listed in the Classes category.
  2. Add the instance variables for the three NSImages as shown here:
    /* TempImageView */
    
    import com.apple.yellow.application.*;
    import com.apple.yellow.foundation.*;
    
    public class TempImageView extends NSImageView {
        protected NSImage coldImage; // add this
        protected NSImage moderateImage; // add this
        protected NSImage hotImage; // add this
  3. Load the images, create NSImages, and assign them to instance variables.
    public TempImageView(NSRect frame) {
            // load images
            super(frame);
            coldImage = new NSImage("Cold.tiff", true);
            if (coldImage == null) {
                System.err.println("Image Cold.tiff not found.");
            }
            moderateImage = new NSImage("Moderate.tiff", true);
            if (moderateImage == null) {
                System.err.println("Image Moderate.tiff not found.");
            }
            String h = NSBundle.mainBundle().pathForResource("Hot", "tiff");
            if (h != null) {
                hotImage = new NSImage(h, true);
            } else {
                System.err.println("Image Hot.tiff not found.");
            }

    There are a few things to observe about the above excerpt of code:

    • TempImageView's constructor is based on NSImageView's NSImageView(NSRect) method, so the first thing done is a call to super's constructor.
    • It uses NSImage's constructor NSImage(String, boolean) to locate the specified image resource in the application bundle and create an NSImage with it.
    • For the third image (just to show how it can be done differently) NSBundle's pathForResource(String, String) method is called to locate the image and return a path to it within the application bundle. This path is used in the NSImage(String, boolean) constructor.
    • The error handling in this constructor is rudimentary. In a real application, you would probably want to implement something more useful.
  4. Set attributes of the image view:
            setEditable(false);
            setImage(moderateImage);
            setImageAlignment(NSImageCell.ImageAlignCenter);
            setImageFrameStyle(NSImageCell.ImageFrameNone);
            setImageScaling(NSImageCell.ScaleProportionally);
    }

The foregoing procedure might lead you to wonder how you can learn more about the methods of a Yellow Box class, especially their arguments and return types. The current versions of Mac OS X and Yellow Box for Windows provide a tool to help you, JavaBrowser. They also include a version of the Java Yellow Box API reference documentation which documents many classes; for those that are not documented, a "skeletal" class specification displays the prototype for each method and includes an HTML link to the method's Objective-C counterpart in the reference documentation.

Implement the Image-Setting Method

TempImageView has one public method that TempController calls whenever the user enters a new temperature value: the tempDidChange method. Implement this method as shown here:

 public void tempDidChange(int degree) {
        if (degree < 45) {
            setImage(coldImage);
        } else if (degree > 75) {
            setImage(hotImage);
        } else setImage(moderateImage);
    }

These ranges are completely arbitrary, but could be influenced by a California climate. If you have lower or higher thresholds for hot and cold temperatures, you can specify your own ranges.

Call the Image-Setting Method

In the convert method of TempController, call TempImageView's tempDidChange method after converting the entered value. Copy the following example:

 public void convert(NSTextField sender) {
    if (sender == celsius) {
        int f = (int)((9.0/5.0 * celsius.intValue()) + 32);
        fahrenheit.setIntValue(f);
    } else if (sender == fahrenheit) {
        int c = (int)((fahrenheit.intValue()-32) * 5.0/9.0);
        celsius.setIntValue(c);
    }
    tempImage.tempDidChange(fahrenheit.intValue()); // add this
    }

You must also add the tempImage outlet you defined earlier in Interface Builder as an instance variable in TempController.java .


Previous | Next
© 1998 Apple Computer, Inc.