Skip to Page Content | Navigation for Module


Navigation for Module 10: Scripts/Java
Page 11 of 19

  1. * Java Accessibility API

Java Accessibility API

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.

Note: when you are looking through the API, you will more than likely run across AccessibleComponent. This is an interface that you may want to explore, especially if you are interested in creating your own components. It will allow assistive technologies to interact with your component in a meaningful manner. Also, if you plan on creating a Swing component, realize that JComponent does not directly implement the Accessible interface, so you will need to do so explicitly; otherwise, you may want to consider extending AccessibleJComponent. Often, this also involves creating an inner class.

Setting Accessible Descriptions

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.

Code example using getAccessibleContext() and setAccessibleDescription()

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();
   }
}
Note: The description should be meaningful, because this description may be the only information that is presented to the user. It is not enough to assume that the user has even limited vision, so any graphical representation that may appear on the screen is almost irrelevant.

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.

Setting Names

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.

Code example for use in constructor of "setting descriptions":

      // How to set the name
      AccessibleContext ac = button1.getAccessibleContext();
      ac.setAccessibleName("Button");

The next page will discuss Java applications.

Top of Page arrow up
       Page 11


 
-- END OF PAGE