Associate labels with controls, objects, and icons.
Rationale
Labels name an object (e.g., icon names, window titles, button labels, edit fields). The labels must be associated programmatically with the object in order for the assistive technology to make them available to the user. If the labels are not associated programmatically, screen readers may use proximity to determine the label for a control or object. Proximity is not an accurate way to determine object labels.
Development techniques
One of the following techniques is the minimum required to meet Checkpoint 2.4 from the IBM Java Accessibility Checklist:
- When using JFC JLabel to label another component, call the setLabelFor method to set the component it is labeling. When you use setLabelFor, the component you label (e.g. a JTextField) will get the AccessibleName and AccessibleDescription of the JLabel.
- 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.
Note: The Java Access Bridge does not support the AccessibleRelation class.
// 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));
AccessibleRelationSet abuttonRelationSet =
aButton.getAccessibleContext().getAccessibleRelationSet();
abuttonRelationSet.add(new AccessibleRelation
(AccessibleRelation.LABELED_BY, myLabel));
//Rest of the application...
Testing techniques
Test the software to ensure that it complies with accessibility requirements.
Tools
You will need to install the following tools to test this checkpoint:
- A screen reader that supports Java.
- One of the Java testing tools such as JavaFerret from Sun or the Java Accessibility Helper from Sun.
Windows techniques
| Action | Result | |
|---|---|---|
| 1 | Test labels with a screen reader.
|
Pass:
The screen reader reads the object or control and the text label associated with it. Both the type of control and the visible label for the control must be read to pass the test. If there is no visible text label, none will be read by the screen reader. Fail: The screen reader reads the input field, but not the text label. For example, tab to an input field labeled "Name". If the screen reader only says "edit" and does not read the label "Name", it is not accessible.Since some techniques such as AccessibleRelation are not supported through the Java Access Bridge, perform the next test to see if the problem is the application or the screen reader. |
| 2 | If the screen reader did not read the text labels, run JavaFerret to determine if this is a problem with the application or the screen reader.
|
Pass:
Fail: JavaFerret displays the name of the object, but not the label associated with it. If the screen reader did not read the label and JavaFerret does not display the label, it is not accessible. |
©2001, 2008 IBM Corporation
Last updated February 15, 2008.
