Developing Java Applications: A Tutorial | Building a Simple Application

Defining the Controller Class

Interface Builder not only lets you construct the user interface of an application from real objects stored on palettes, but lets you partially define a class in terms of its name, its superclass, its outlets, and its actions.

Identify the Class and Its Superclass

You define a custom class using the Classes menu and the Classes display of the nib file window.

  1. Click the Classes tab of the TemperatureConverter nib file window.
  2. Highlight java.lang.Object in the list of classes.
  3. Choose Subclass from the Classes menu.

    "MyObject" appears in an editable field below java.lang.Object.

  4. Type "TempController" in place of "MyObject" and press Return.

Specify the Outlets of the Class

An outlet is a reference one object holds to another object so that it can easily send that object messages; it is an instance variable of type id or IBOutlet . The TempController class has two outlets, one to each of the text fields in the user interface.

  1. Click the small electric-outlet icon to the right of TempController in the Classes display.

    Tthe area under TempController expands to include "Outlets" and "Actions."

  2. Select "Outlets."
  3. Choose Add Outlet from the Classes menu.

    You can press the Return key instead of choosing the menu command.

  4. Type "celsius" in place of "myOutlet."

Repeat steps 2 through 4, this time naming the outlet "fahrenheit".

To collapse the TempController item, click any other class. in the Classes display.

Specify the Action of the Class

An action refers to a method invoked in a target object when a user event occurs, such a the click of a button or the movement of a slider. We want a method in TempController to be invoked whenever the user presses the Return key in a text field.

  1. Click the small target icon to the right of TempController in the Classes display.

    The area under TempController expands to include "Outlets" and "Actions."

  2. Select "Actions."
  3. Choose Add Action from the Classes menu.

    You can press the Return key instead of choosing the menu command.

  4. Type "convert" in place of "myAction" (parentheses are automatically appended to the method name).

See the illustration above for an example of what things look like when you complete this task.

Related Concepts

Previous | Next
© 1998 Apple Computer, Inc.