Interfacing with Accessibility

The following code shows how to create a simple assistive technology that can monitor events on the system event queue and interface with accessible components. The example consists of one class, AssistiveExample. This class creates a small window containing two labels and five checkboxes, which are repeatedly updated when the mouse comes to rest over an accessible component for longer than half a second.

Note that while using 1.2 or higher accessibility, we have to check to see if the GUI is ready for us to start firing accessibility-related commands. We do this by checking the EventQueueMonitor.isGUIInitialized( ) method. This method returns a boolean indicating whether the GUI will accept accessibility commands. If it does, then we’re fine. If it doesn’t, then we must register ourselves to be notified when the GUI becomes available. This uses the GUIInitializedListener interface, which we explained earlier.

To use the AssistiveExample class, simply create a new AssistiveExample object from an existing application. The constructor creates a frame and makes it visible. For an example, check the source of the BigExample class in the online code files for this chapter.

Finally, note that we have a single button in our assistive example that performs the first action reported by the accessible context. You can use the Tab key to bring this button into focus while pointing with the mouse, then press the space bar to fire the action.

// AssistiveExample.java ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.