Associate labels with controls, objects, icons, and images. If an image is used to identify programmatic elements, the meaning of the image must be consistent throughout the application.
This page provides specific examples to implement the Software techniques for providing labels using Java Swing. For an explanation of this requirement, see Rationale for labels in the Software checklist.
On this page:
Required development techniques
The following techniques are the minimum required to meet Checkpoint 2.3 from the IBM Software Accessibility Checklist:
- Labels must be associated with all user controls, objects, window titles, icons and images.
- All images, that are not decorative and have meaning, must be used consistently through the application.
Examples for Java developers
1. Labels must be associated with all user controls, objects, window titles, icons and images.
When using JFC JLabel to label another component, call the setLabelFor method to set the component it is labeling. When you use setLabelFor method, the component you label (e.g. a JTextField) will get the AccessibleName and AccessibleDescription of the JLabel.
// Create label and explicitly set AccessibleName JLabel myLabel = new JLabel("A column of buttons"); // Create buttons JButton aButton = new JButton("Button A"); aButton.getAccessibleContext().setAccessibleName("Button A"); JButton bButton = new JButton("Button B"); bButton.getAccessibleContext().setAccessibleName("Button B"); JButton cButton = new JButton("Button C"); cButton.getAccessibleContext().setAccessibleName("Button C"); // Create a LABELED_BY relationship //that indicates that "myLabel" //is the label for "aButton" AccessibleRelationSet labelRelationSet = myLabel.getAccessibleContext().getAccessibleRelationSet(); labelRelationSet.add (new AccessibleRelation(AccessibleRelation.LABEL_FOR, aButton)); //The JLabel.setLabelFor() method as a handy shortcut to set the LABEL_FOR relationship between a label and the object that it's labeling. AccessibleRelationSet abuttonRelationSet = aButton.getAccessibleContext().getAccessibleRelationSet(); abuttonRelationSet.add (new AccessibleRelation(AccessibleRelation.LABELED_BY, myLabel)); //Rest of the application...
Applications written for Java 1.3 or later can also support accessible relationships defined in the AccessibleRelation class. For example, you can define an AccessibleRelation of AccessibleRelation.LABEL_FOR to define a label for a specific component. This would be followed by adding an AccessibleRelation.LABELED_BY for the target component.
2. All images, that are not decorative and have meaning, must be used consistently through the application.
When bitmap images are used to identify controls, status indicators or other programmatic elements, the meaning assigned to those images must be consistent throughout an application. See the following samples that illustrate some of these techniques: JAccessibleNameSample and JIconSample.
Required test techniques
Test the software to ensure it complies with accessibility requirements.
Required test software
Install the following software to test this checkpoint.
There are no unique steps for testing this checkpoint. Follow the required test techniques in Software checkpoint 2.3.
If the screen reader test fails for any user interface element, the problem must be debugged with an accessibility API validation tool. These techniques can be used to help determine if a problem is caused by a coding error in the application, or if it is caused by a screen reader defect.
|Action||Results for compliance|
Additional information on how to use the API validation tool is included with the tool.
©2009 IBM Corporation
Last updated September 29, 2009.