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.
To generate the source-code templates files for TempController, in Interface Builder:
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.
In Project Builder, perform the following steps to modify the generated files:
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){ } }
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
.
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.
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:
applicationDidFinishLaunching
methodThe 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.