BBj Custom Objects Tutorial: Program #6 - Callback Choices

Example Programs

Here are some example BBj programs. Each example is designed to demonstrate some aspect of using Custom Object Classes in BBj.

The complete source code for each program is included in an appropriately named sub-folder in the zip file you can download here.

Program #6: Callback Choices

Let’s take a look at a BBj program that demonstrates how to use classes when setting event callbacks for graphical user interface (GUI) programming. First, in Reporter.bbj, we create a simple Reporter class to show how a custom object class can hold methods for callbacks. Then, in Dialog.bbj, we create a Dialog class that encapsulates the GUI code necessary to display a window with callbacks. Finally, we create the Callbacks.bbj code to use the Reporter and Dialog classes. Again, we show the code for each new source file using a different border color for readability:

Execution

Creating the BBj source files and then running Program 6 (the code in Callbacks.bbj) resulted in the following SysConsole output after these steps:

  1. Push the “Push me!” button

  2. Click in the InputE edit field at the bottom of the dialog window

  3. Click the “Push me!” button

Figure 6. The Output from Program 6 After Push-Click-Push

At this point, close the dialog window (you can click the “X” button in the upper right corner) to get this final SysConsole output:

Figure 7. Output After Closing the Callbacks Dialog

Observations

Here are some details that this code demonstrates:

  • A program can register a callback for an event where the callback is a method on a custom object (as opposed to a label within a program). In Dialog.init(), the call to setCallback() registers a callback for ON_BUTTON_PUSH using #this! as the object and the method Toggle() as the method for the event callback.

  • A program can register a callback for an event where the method is on a different custom object. In the Dialog.registerEvents(reporter!) call, the code registers a handler for the ON_LOST_FOCUS and ON_GAINED_FOCUS events with the methods of a different custom object (reporter!).

  • A method on a custom object class can register a callback for an event where the callback is to a standard BBj label defined inside of that method. In the Dialog.show() call, the code registers a handler for the window’s ON_CLOSE event with the doReturn label. The label must be defined within the method where setCallback() is being called. Code within a method cannot find labels that are not within the same method.

  • The callback for ON_CLOSE destroys the window and then uses methodret to return to the calling program. After returning from show(), we are no longer doing process_events.

  • When registering a custom object method for a callback, that method must accept a single parameter and the event for which the method is being registered must be assignable to the type of that parameter. We can register the method Toggle(BBjButtonPushEvent p_event!) for the ON_BUTTON_PUSH event because the type of parameter is the same as the type of the event that is generated.

  • You could register the method report(BBjSysGuiEvent p_event!) for any button control event that extends the type of BBjSysGuiEvent (most if not all of them).

  • We are able to gather information while displaying the dialog, and then retrieve that information from the dialog after the window has been destroyed.

  • A declare statement within a method body (as in Dialog.Toggle()) defines the type of a local variable within that method body. Such local variables are not visible to code outside of that method.

Next Step: Example Program #7

BBj Custom Objects Tutorial Contents

BBj Custom Objects Tutorial: Introduction

BBj Custom Objects Tutorial: Interfaces

BBj Custom Objects Tutorial: Classes

BBj Custom Objects Tutorial: Fields

BBj Custom Objects Tutorial: Methods

BBj Custom Objects Tutorial: Using Custom Objects

BBj Custom Objects Tutorial: Program #1 - Writing a Check

BBj Custom Objects Tutorial: Program #2 - Protected and Private Fields

BBj Custom Objects Tutorial: Program #3: The Static Keyword

BBj Custom Objects Tutorial: Program #4 - Error Handling

BBj Custom Objects Tutorial: Program #5 - Class Inheritance

BBj Custom Objects Tutorial: Program #6 - Callback Choices

BBj Custom Objects Tutorial: Program #7 - Constructors and Field Initialization