Skip to main content

4.0 Using the JFC to Build Accessible Applications

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:

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:

  1. 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.

  2. Describe Icons and Graphics:
  3. Label Components:
  4. 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:
  5. 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.
  6. 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.
  7. 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.
  8. 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:

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

AttributeSet

and

MutableAttributeSet

are JFC classes that define an interface to an attribute set.

StyleConstants

defines 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:

AttributeSet / StyleConstants Classes
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.

It is helpful if you name the set of attributes as they pertain to specific sets within your document.

For example, if you are building a web browser, the

NameAttribute

for <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

JComponent

and is a descendant of the AWT container class.

JComponent

is 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:

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

JComponent

as your base component.

Next: 5.0 Building Custom Accessible Components