4.0 Using the JFC to Build Accessible Applications
If you are a typical software engineer and take a glance at this text, you'll find it is easier to use the JFC than to implement the Accessibility API. Make it easy on yourself and use the JFC!
This section discusses how you use the JFC to build your accessible Java application. It is broken down as follows:
- Using the JFC Components Unmodified
- Enhancing JTextComponents
- Changing Data Models and Cell Renderers
- Creating New Components Based on the JFC
4.1 Using the JFC Components Unmodified
The JFC is an extremely rich class library that has implemented both keyboard accessibility as well as the accessibility API. If you use JFC "as is", there are only a few simple things you need to do:
- Provide Descriptive Component Text: Set the accessible description on all
components; see section 2.9 Describe Icons and
Graphics .
Hint: Some JFC components use the tool tip for this information in the absence of you setting an accessible description. So, if you already set the tool tip text, this task might be complete.
- Describe Icons and Graphics:
- For JLabels and JButton classes that contain only images, set the AccessibleName; see section 6.2.1.1 AccessibleName.
- Whenever you use a JFC setIcon method, use a JFC ImageIcon. Whenever you use a JFC ImageIcon, use its setDescription method to help blind users. ImageIcons are used in a variety of components, ranging from JFC buttons to labels. You must also remember to do this with images inserted into a JTextComponent or with JFC components sub-classed from JTextComponent.
- Label Components:
- When a JLabel is used to label another component, call its setLabelFor method to set the component it is labeling. This can be null if this does not label a component. When you use setLabelFor, the component you label (e.g. a JTextField) will get the AccessibleName and AccessibleDescription of the JLabel. If both the displayedMnemonic and labelFor properties are set, the label requests focus to the component specified by the labelFor property when the mnemonic is activated. Section 2.3 Providing Focus provides details about requesting the keyboard focus.
- When your application is targeted for Java 1.3 establish AccessibleRelations for labeling between your JLabel and your target labeled component. See section 2.8.1 Label Components for detailed information.
- Enable Keyboard Access: If you build JFC applications, without customizing components, there is little to do since keyboard access is built in. There may be times when you need to add keyboard access features such as through the use of mnemonics and accelerators. To enable keyboard access for JFC you should:
- Follow the essential keyboard access programming practices defined in section 2.1 Make Your Application Keyboard Accessible.
- Add Keyboard Activation of Child JFrames or JInternalFrames. If you build a Java application or applet that manages child frames, provide a keyboard equivalent to let the user cycle the focus to each child frame, or let the user access these frames from a keyboard accessible main menu.
- Group Objects Inside Named Panels: It is important for you to logically group components and assign that grouping a name. This facilitates navigation of your application. You can accomplish this by adding components to JPanels and setting the AccessibleName on the JPanel. If you use a JPanel with a JFC TitledBorder there is no reason to set the AccessibleName because the JFC will set the name to the title text. For more information, see section 2.8.2 Name Logical Groups.
- Set the Focus: Make sure some component in your application has the input focus at all times. When keyboard enabling your application, this should take care of itself. Most of the JFC components implement focus setting to support keyboard navigation. See section 2.3 Providing Focus for additional information on setting the focus.
- Be Sufficiently Multi-Threaded: Make sure your program is adequately multi-threaded to prevent a situation that inhibits a concurrent assistive technology from running. For example, if your client application is dependent on a server action, design your software to let other threads run, even if you are waiting for the server to respond. For more information on making your application multi-threaded for accessibility see section 2.6 Multi-Threading Your Application - Be Access Aware.
- Provide an Accessible Layout: When developing the visual layout of your components, provide a logical layout which is determined by the order in which your components are added. The logical layout effects the keyboard tabbing sequence and the order an assistive technology uses to read your application. Assistive Technologies can use the order with which components are added to determine the logical order to use to speak application components. This is also an order for which the Java keyboard tabbing sequence depends. See section 2.7 Provide a Logical Component Layout for more details.
In short, these simple programming functions let you meet essential accessibility programming requirements. JFC takes care of the rest!
4.2 Enhancing JTextComponents
JTextComponent is a class with an extendable document model. Each element within the document model needs only to support a common element interface. The element could be anything from another JFC component to simple text. Furthermore, the document model itself has a pre-defined interface. This design allows you to embed documents within documents, insert other complex components, and so on.
What enables assistive technologies, such as a screen reader program, to decipher this is:
- Maintaining the Accessible and AccessibleText interfaces support in JTextComponents.
- Maintaining the Accessible and AccessibleHypertext interfaces for JTextComponents supporting hypertext.
- Standardizing attributes using the Swing AttributeSet and StyleConstants classes for each indexed character.
Unless you modify the JTextComponent, AccessibleText or AccessibleHyperlink implementations, these interfaces still allow an assistive technology to read your "basic" document. JTextComponent implements AccessibleHypertext on Swing editor kits that supports hypertext, allowing assistive technology to enumerate and activate the links in your document. Developers can also break the accessibility of documents by improperly implementing the AttributeSet and its attribute elements.
4.2.1 AttributeSet, MutableAttributeSet, and StyleConstants Classes
AttributeSetand
MutableAttributeSetare JFC classes that define an interface to an attribute set.
StyleConstantsdefines a common set of attributes for the attribute set. Without a known set of attributes, assistive technologies will not know what to do with your document, much the same way that component technology fails without a set of standards.
In implementing your AttributeSet for document elements, it is important that you define attributes using those pre-defined in the StyleConstants class. Use the following list of Key/Value pairs:
| StyleConstant Attribute Keys | Class Definition for the Value |
|---|---|
| ALIGN_CENTER | Boolean |
| ALIGN_JUSTIFIED | Boolean |
| ALIGN_RIGHT | Boolean |
| Alignment | ALIGN_CENTER, ALIGN_JUSTIFIED, or ALIGN_RIGHT |
| Background | Color |
| BidiLevel | Integer Bidirectional level as assigned by the Unicode bidi algorithm |
| Bold | Boolean |
| ComponentAttribute | Component or JComponent that must implement Accessible |
| ComponentElementName | String |
| ComposedTextAttribute | AttributedString that represents the composed text. |
| FirstLineIndent | Float |
| FontFamily | String |
| FontSize | Integer |
| Foreground | Color |
| IconAttribute | ImageIcon |
| IconElementName | String |
| Italic | Boolean |
| LeftIndent | Float |
| LineSpacing | Float |
| Model Attribute | Object defining the model for embedded objects that have a model view separation. |
| NameAttribute | String |
| Orientation | String representation of orientation |
| ResolveAttribute | AttributeSet |
| RightIndent | Right |
| SpaceAbove | Float |
| SpaceBelow | Float |
| StrikeThrough | Boolean |
| Subscript | Boolean |
| Superscript | Boolean |
| Tabset | TabSet |
| Underline | Boolean |
Table 3.0 AttributeSet Key/Value Pairs Defined in StyleConstants.java
If you are converting an existing program to JFC, the AttributeSet and MutableAttribute interfaces should allow you to convert your existing document structure to a JFC implementation. These interfaces provide for text runs, and embedded components. StyleConstants.java has a provision for embedded components that allows you to embed components in your document elements; you can add things like spreadsheets, and tables. Furthermore, you can define your own user interface using JFC to give whatever look you want to your document. Also, JFC comes with source to show you different implementations other than plain text areas.
ATTENTION
It is essential that when you add components to your document model, the components
implement the Accessible Interface. If you use standard JFC components, they already do.
For example, if you are building a web browser, the
NameAttributefor <H1> should be the string "<H1>." This can help an assistive technologies tailor themselves for your application. For example, a screen reader program could be designed to navigate the HTML tags of a web document.
4.3 Changing Data Models and Cell Renderers
The root component class is called
JComponentand is a descendant of the AWT container class.
JComponentis the root class of almost all JFC classes.
The design of each component is based on the Model-View-Controller (MVC) architecture. Each component is broken into three parts:
- Model or "data" portion of the component.
- User Interface or "look and feel" portion, also called the view.
- Controller portion, which manipulates the components.
Implementation of the Accessibility API is triggered from the "data portion" of each JFC component. This lets you implement an accessible application without affecting its "look and feel."
When developing your application using the JFC, it is important that you avoid modifying or replacing the "data model portion" of each component with your own. You run the risk of improperly implementing methods that are required to support the interfaces and this could break the accessibility features of the component.
Another JFC concept is a cell renderer. A cell renderer is an interface that a JFC component uses to define a component drawing object within a list of elements. For example, the JFC uses ListCellRenderers for JList elements and TableCellRenderers for JTable elements. For a given cell renderer, a method is provided to return a component configured for a given element, like getListCellRendererComponent in the JList component.
If you change the cell renderer for a JFC component, make sure it implements Accessible. If you implement your cell renderer on an existing JFC component, you can configure your cell renderer for the given element and reuse an already accessible component. An example of this is how JList uses JLabel to render its children.
4.4 Creating New Components Based on the JFC
If you need to build completely new components follow section 5.0 Building Custom Accessible Components by starting with a
JComponentas your base component.
