Developing Java Applications: A Tutorial | Building a Simple Application

Implementing the Controller Class

You're now ready to generate source-code template files from the nib file you've created with Interface Builder. After that, you'll work solely with the other major development application, Project Builder.

Generate the Source Code Files

To generate the source-code templates files for TempController, in Interface Builder:

  1. Click the Classes tab in the nib file window.
  2. Select the TempController class.
  3. Choose Create Files from the Classes menu.
  4. Respond to the query "Create TempController.java?" by clicking Yes.
  5. Respond to the query "Insert file in project?" by clicking Yes.

Interface Builder creates a TempController.java file and puts it in the Classes category of Project Builder. You can now quit Interface Builder (or, better still, hide it) and click in Project Builder's project window to bring it to the front.

Modify the Source Code Files

In Project Builder, perform the following steps to modify the generated files:

  1. Click the Classes category in the left column of the project browser.
  2. Click TempController.java in the second column.

    The following code is displayed in the code editor:

    import com.apple.yellow.application.*;
    import com.apple.yellow.foundation.*;
    public class TempController {
        Object celsius;
        Object fahrenheit;
        public void convert(Object sender){
    }
    }
  3. Modify the above code so that it looks like this:
    import com.apple.yellow.application.*;
    import com.apple.yellow.foundation.*;
    public class TempController {
        NSTextField celsius;
        NSTextField fahrenheit;
        public void convert(NSTextField sender) {
        }
    }

Why is this modification necessary? Java is a strongly typed language and has no equivalent for the Objective-C dynamic object type id . When Interface Builder generates source-code files for Objective-C classes, it gives id as the type of outlets and as the type of the object sending action messages. This id is essential to the method signature for outlets and actions. However, when it generates Java source-code files, it substitutes the static Java type Object for id .

Related Concepts

Implement the convert Method

Finally implement the convert method in Java, as shown here:

import com.apple.yellow.application.*;
public class TempController {
    NSTextField celsius;
    NSTextField fahrenheit;
    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);
        }
    }
}

You can freely intermix Yellow Box and native Java objects in the code. And you can use any Java language element, such as the try/catch exception handler.

Write a Patch for Windows Applications

If you're writing the application to run on Yellow Box for Windows, you must write some code that works around a problem with the Java virtual machine (VM) on Windows. Because the way the VM implements security-manager features, it will otherwise not allow any native method to be loaded through a class loader.

The best place to put the code shown below is in:

The code to add is the following:

try {
    com.apple.security.NullSecurityManager.installSystemSecurityManager();
} catch (Exception e) {
    // Can't install it
}

If the exception is raised, the "null" security manager cannot be installed and thus native code might not be invoked properly.


Previous | Next
© 1998 Apple Computer, Inc.