Skip to main content

Event Handling

BBj programs respond to user actions -- button clicks, text changes, window closes -- through event callbacks. You register a callback method or label on a control, then call process_events to start the event loop. BBj dispatches events to your callbacks as they occur.

At a Glance

FeatureSyntaxPurpose
setCallback (method)ctrl!.setCallback(event, obj!, "method")Register OO callback with event parameter
setCallback (label)ctrl!.setCallback(event, "label")Register label-based callback
process_eventsprocess_eventsStart the event loop (blocks until event)
getLastEvent()BBjAPI().getSysGui().getLastEvent()Get event object in label-based callbacks
ON_BUTTON_PUSHctrl!.ON_BUTTON_PUSHButton click event constant
ON_CLOSEwindow!.ON_CLOSEWindow close event constant
ON_EDIT_MODIFYctrl!.ON_EDIT_MODIFYText change event constant

BBj event handling evolved through three generations. READ RECORD polling loops (Visual PRO/5 era) gave way to the CALLBACK verb (procedural BBj), and finally to setCallback (modern OO BBj). This chapter teaches setCallback first as the recommended approach, with legacy patterns in a separate reference page for reading existing code.

For Java, Python, and C# Developers

TaskJavaPythonC#BBj
Register handlerbtn.addActionListener(l)btn.bind("<Click>", fn)btn.Click += handler;btn!.setCallback(btn!.ON_BUTTON_PUSH, obj!, "onClick")
Handle clickvoid actionPerformed(ActionEvent e)def on_click(event):void OnClick(object s, EventArgs e)method public void onClick(BBjButtonPushEvent ev!)
Event loopSwing EDT (automatic)root.mainloop()Application.Run()process_events
Remove handlerbtn.removeActionListener(l)btn.unbind("<Click>")btn.Click -= handler;btn!.clearCallback(btn!.ON_BUTTON_PUSH)

BBj uses setCallback with a string method name and an object reference. The event handler method receives a typed event parameter (e.g., BBjButtonPushEvent). For the complete cross-language reference, see BBj for Java, Python, and C# Developers.

Complete Runnable Examples

This chapter's code snippets illustrate individual concepts. For complete, runnable programs you can open directly in the BBj IDE, see the sample files in samples/10-event-handling/:

  • setcallback_labels.bbj -- Label-based setCallback with getLastEvent()
  • setcallback_methods.bbj -- Method-based setCallback with typed event parameters
  • event_types.bbj -- Multiple event types (button push, edit modify, focus)
  • contact_form.bbj -- Complete contact form with shared callback pattern

See Running Samples for setup instructions.