Chapter 24. Accessibility
1. Java 2 Accessibility API overview
2. Following accessibility guidelines
3. Accessibility support for custom components
In this chapter:
- Java 2 Accessibility API overview
- Following accessibility guidelines
- Accessibility support for custom components
24.1 Java 2 Accessibility API overview
When developing an application, it is important to keep in mind that many of it's users may be disabled to some degree. Disabilities can range anywhere from poor vision to blindness, hearing impared to deafness, or a physical inability to use a standard input device such as a mouse or keyboard. Many of these users may often rely on special devices plugged into the computer to help offset specific disabilities. Some of these devices can substitute printed text with audio output, ease keyboard input or point-and-click functionality, perform large scale screen magnification, etc. To do their job correctly, most of these devices need to find connection points within applications, and substitute some of the original functionality with accessible variations.
Java offers sets of classes and interfaces to provide the needed connection points for such special devices. These are referred to as assistive technologies, and are normally packages in a JAR archive. Java loads assistive technologies when the VM is started by looking for special entries in a file called accessibility.properties located in the jdk1.2\jre\lib directory. We will discuss how to use this file below.
24.1.1 Implementing accessibile applications
Fortunately, Swing provides an extremely rich API for accessibility needs. If we use Swing "as is", there are only a few simple things we need to do in order to provide an accessible interface for assistive technologies:
Use tooltips to provide a text description of a component's purpose. The tooltip text may be used by accessible devices to provide a description of that component.
Enable keyboard access. Ideally, all functionality should be available without using the mouse. Use mnemonics for buttons and menus, set keyboard accelerators in menus.
Associate labels with components. By setting both the displayedMnemonic and labelFor properties, a label will transfer focus to the specified component with the labelFor property when the displayedMnemonic is activated (i.e. when ALT + mnemonic character is pressed).
Logically group components with labels refering to each when necessary.
Throughout this book we have adhered to most of these rules without explicitly being aware that we were also building accessible-compliant applications. Let's review a bit more about the association of labels with components. Consider the following code:
JPanel p = new JPanel();
p.add(new JLabel("IP address:"));
JTextField user = new JTextField();
To make this code accessible we would need to associate the label with the text field above as follows:
JPanel p = new JPanel();
JLabel lbl = new JLabel("IP address:");
JTextField user = new JTextField();
The changes made should be familiar and are relatively simple. First, the setDisplayedMnemonic() method assigns a mnemonic character to the label component (this character will be underlined in the label if the label's text contains that character). Second, a tooltip text is assigned to the text field to provide an accessible text description. Finally, the setLabelFor() method is called to associate the text field component with the label. When the mnemonic is activated, the label will notify the text field to request the focus.
Java 2 ships with the javax.accessibility package, which includes classes and interfaces constituting the Java Accessibility API. In the remainder of this section we'll give a brief introduction to some of its most important constituents.
24.1.3 The Accessible interface
abstract interface javax.accessibility.Accessible
This interface must be implemented by all Java classes designed to support accessibility. All Swing components implement this interface either directly or indirectly. It declares only one method, getAccessibleContext(), which returns an instance of AccessibleContext (see below).
abstract class javax.accessibility.AccessibleContext
This class provides the minimum information all accessible objects should expose. This information includes the accessible: name, description, role, and state of the object, as well as information about the parent and children of the object. To obtain more specific accessibility information about a component this class exposes methods to retrieve the instances of interfaces defined in the accessibility package (see below).
Typically Swing components define an inner class which extends AccessibleContext and provides information specific to that component. For instance, JComponent provides the inner class AccessibleJComponent, JComboBox provides AccessibleJComboBox etc.
This class defines several static constants used in providing information about the role a particular component plays in an application. Normally the getAccessibleRole() method of AccessibleContext will return one of these constants.
24.1.6 The AccessibleAction interface
abstract interface javax.accessibility.AccessibleAction
This interface declares methods to perform one or more accessible actions, retrieve descriptions of these actions, and find out exactly how many actions have been exposed to assistive technologies.
int getAccessibleActionCount(): returns the number of actions exposed through the doAccessibleAction() method.
String getAccessibleActionDescription(int i): should be defined to return a description of the code executed by doAccessibleAction() based on the given index.
boolean doAccessibleAction(int i): When implementing this interface we are expected to define code to be executed based on a given index. Normally this is involves invoking event handling code defined for a specific component, however, there is nothing stopping us from defining accessible-specific code here. It should return a boolean value representing whether or not the given index corresponds to a valid action.
24.1.7 The AccessibleComponent interface
abstract interface javax.accessibility.AccessibleComponent
This interface should be supported by any accessible component that is rendered to a graphical context. It provides the standard mechanism for an assistive technology to determine and assign certain aspects of the graphical representation of that component. Most methods will call the corresponding component's methods where applicable. (Note that no painting methods are members of this interface because we are expected to define our own look-and-feel if a more customized view is desired.)
24.1.8 The AccessibleHypertext interface
abstract interface javax.accessibility.AccessibleHypertext
This interface provides standard mechanisms for determining and manipulating hyperlinks with an assistive technology. Normally only text components will provide implementations of this interface, however, any other component supporting hypertext may do the same.
24.1.9 The AccessibleSelection interface
abstract interface javax.accessibility.AccessibleSelection
This interface should be implemented by any accessible component that has selectable children, whether individually or in groups, such as trees, tables, lists, tabbed panes, menu bars, etc.
24.1.10 The AccessibleText interface
abstract interface javax.accessibility.AccessibleText
This interface should be implemented by any accessible text component, and is intended to provide access to that component's document content and attributes (where applicable).
24.1.11 The AccessibleValue interface
abstract interface javax.accessibility.AccessibleValue
This interface should be implemented by any accessible component that supports a numerical value (e.g. a scroll bar). It is intended to be used by an assistive technology to determine and assign current, minimum, and maximum values.
24.1.12 Accessibility Utilities
In order to provide accessibility compliance between an application and an assistive technology, we cannot just rely on our use of the accessibility package. We also require support for locating the objects that implement accessibility functionality, as well as support for loading assistive technologies into the Java virtual machine and tracking all events sent to the system event queue. For these reasons, Sun provides the Java Accessibility Utilities which can be downloaded from the JavaSoft web site. Along with utility classes to perform these services, a set of sample applications is included which can be used to test and debug accessible Java programs. These applications are capable of linking with the system event queue and displaying information about selected types of events.
The Java Accessibility Utilities package is an assistive technology itself. To install it we must place the jaccess.jar and jaccess-examples.jar files into our jdk1.2\jre\lib\ext directory.
To use one of its assistive-technology examples, we need to include a string such as the following in our accessibility.properties file:
This will cause the Explorer utility to start each time we start a new JVM. (Note that if this file does not already exits we must create it ourselves.) In the examples below we assume that you have enabled the Explorer utility, and it is shown in the corresponding example figures.