A component is considered to be accessible if it implements the Accessible interface. As mentioned before, Java 1.2 introduced Swing
at the same release as the initial accessibility API, and therefore, by default, most Swing components are partially accessible. In Java 1.3,
the accessible components list was extended to included the traditional AWT components. The Accessible interface defines only one method -
getAccessibleContext( ), which returns an instance of an AccessibleContext.
The AccessibleContext class is the core class you will be working with on this page. It represents the minimum information needed for accessibility. By minimum information, it is meant that the name of the component, its description (or alternative text), as well as its role and state information. Fortunately, you can pull an AccessibleContext from almost all components, and use standard 'get' and 'set' function calls to access and modify the accessibility properties. There are primarily two fields you will be interested in modifying:
Both are discussed below. It is not important that you understand too many details beyond what is discussed below to achieve an accessible applet or application.
Perhaps the most important thing you can do for a component is to provide a meaningful description. This can be achieved by getting
the components AccessibleContext, then calling the setAccessibleDescription() method.
import javax.swing.*;
import javax.accessibility.*;
class AccessibleExample1 extends JFrame {
JButton button1;
 
AccessibleExample1() {
super ("AE1");
button1 = new JButton("Button 1");
// Here is where we set the description!
AccessibleContext ac = button1.getAccessibleContext();
ac.setAccessibleDescription("This button does nothing");
this.getContentPane().add(button1);
this.pack();
this.setVisible(true);
}
public static void main (String args[]) {
AccessibleExample1 ae1 = new AccessibleExample1();
}
}
You can verify that a description has been set by using the
getAccessibleDescription() method. If the description has not been set for a
component, this method will return null; and do not forget that you need to
work through the accessible context to do this.
In Swing, components are given default accessible names, so often additional code is not necessary. There are times that a more
appropriate name may need to be given, and if so, using the setAccessibleName( ) from the AccessibleContext instance is the approach
you should use.
// How to set the name
AccessibleContext ac = button1.getAccessibleContext();
ac.setAccessibleName("Button");
The next page will discuss Java applications.