Book Home Java Enterprise in a Nutshell Search this book

3.23. Accessibility

The term accessibility refers to the architectural features of Swing that allow Swing applications to interact with assistive technologies, such as a visual macro recorder that allows users to automate repetitive point-and-click tasks or a screen reader.

To enable accessibility, every Swing component implements the Accessible interface, which, like all accessibility-related classes, is part of the javax.accessibility package. This interface defines a single getAccessibleContext() method that returns an AccessibleContext object for the component. The methods of AccessibleContext export salient information about the component, such as a list of its accessible children and its name, purpose, and description. An assistive technology can use the tree of AccessibleContext objects to gather information about a GUI and assist the user in interacting with that GUI.

A number of the AccessibleContext methods return objects that implement specialized interfaces to return specific types of accessibility information. For example, if an accessible component represents a numeric value of some sort (say a JSlider), the getAccessibleValue() method of its AccessibleContext object returns an AccessibleValue object that provides more information about that value and allows the assistive technology to query and set the value.

The interfaces and classes of the javax.accessibility package provide methods that allow an assistive technology to "read" a GUI. Many of the methods defined by these interfaces duplicate functionality already provided by Swing components. The point, however, is that java.accessibility defines a standard API for interaction between any assistive technology and any accessible application. In other words, the accessibility API is not Swing specific. You can write JavaBeans and other custom components so that they support accessibility. If you do, these components automatically work with assistive technologies.

The details of the javax.accessibility package are of interest to programmers who are creating assistive technologies and developing accessible components or JavaBeans. Unfortunately, the details of these tasks are beyond the scope of this book.

Most of us are not developing assistive technologies and only rarely do we have to create accessible components. What we all want to do, however, is create accessible applications. Since all Swing components support accessibility, it is quite simple to create an accessible application with Swing. The key to supporting accessibility is providing the necessary information that allows an assistive technology to interpret your GUI for a user. The most commonly used example of an assistive technology is a screen reader for the vision impaired. A screen reader needs to be able to verbally describe a GUI to a user who cannot see it. In order to do this, it needs to have names and descriptions for all the critical components in your GUI.

The easiest way to assign a description to a component is to give it a tooltip. This way, your accessibility information also serves as context-sensitive help for novice users:

continue.setToolTipText("Click here to continue");

If, for some reason, you want to assign an accessible description to a component without giving it a tooltip, you can use code like this:

continue.getAccessibleContext().setAccessibleDescription("Continue button");

It is also helpful to assistive technologies if you provide names for your various components. A name should be a short human-readable string that uniquely identifies the component, at least within the current window or dialog box. Buttons, labels, menu items, and other components that display labels simply use those labels as their accessible names. Other components need to have names assigned. Here is one way to do that:

JTextField zipcode = new JTextField();
zipcode.getAccessibleContext().setAccessibleName("zipcode");

In a GUI, important components that do not display their own labels are often associated with JLabel components that serve to identify them. When this is the case, you can use the setLabelFor() method of JLabel to set the accessible name of the other component. The code might look like this:

JLabel zipcodeLabel = new JLabel("Zipcode");
JTextField zipcode = new JTextField();
zipcodeLabel.setLabelFor(zipcode);

By taking the simple step of assigning names and descriptions to your GUI components, you ensure that your application can be interpreted by assistive technologies and successfully used by all users.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.