Responding to Simple System Events
On the Mac OS platform, applications can respond to simple events passed to it by the Finder or other applications. For example, if the user drops a file onto an application's icon, the Finder launches the application (if not already open) and passes it an "open file" event instructing it to open the file if possible.
MRJToolkit allows your Java application to respond to the following system events:
- Note
- Events on the Mac OS platform are handled through Apple Events, which are described in detail in Inside Macintosh: Interapplication Communication. No knowlege of Apple Events is needed when using MRJToolkit.
MRJToolkit also allows your application to respond to an "About box selected" event when the About item in the Apple menu is selected. An About box is a window that gives information about the application, such as the version number, serial number, and credits (members of the development team and so on).
- requests to open a document
- requests to print a document
- requests to quit
To make your application aware of the event, you must implement an interface that handles the particular event and then register the handler method with MRJToolkit. For example, to create a quit handler, you must implement the interface
MRJQuitHandler
. This interface has the form shown in Listing 1-5.Listing 1-5 The MRJQuitHandler interface
package.com.apple.mrj public interface MRJQuitHandler { public void handleQuit(); }To use this interface, the class that implements it must contain a method namedhandleQuit
that defines the actions to take when the application receives a quit request. In addition, the application must also register the method name with MRJToolkit by calling the appropriate registration method in the classcom.apple.mrj.MRJApplicationUtils
. For example, to register the quit event handler, you must call theMRJRegisterQuitHandler
method. Listing 1-6 shows an example of implementing a quit handler.Listing 1-6 Implementing the quit event handler
import com.apple.mrj.*; import java.io.*; ... class QuitTest implements MRJQuitHandler { public QuitTest() { System.out.println("Select Quit from the Apple menu or shutdown to test event handler."); MRJApplicationUtils.registerQuitHandler(this); } public void handleQuit() { System.exit(0); /* Quit the MRJ runtime */ } }Since the call toregisterQuitHandler
occurs within the class that containshandleQuit
, it can use thethis
variable to reference the handler.Table 1-1 shows the available handler interfaces, their corresponding registration methods, and the conditions under which an event is sent. All the registration methods are members of the class
com.apple.mrj.MRJApplicationUtils
.