Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...

Swing Chapter 24. (Special topics) Accessibility. Easy for reading, Click here!

Custom Search
Swing Chapter 24. (Special topics) Accessibility. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 1/3 

Next Page (2/3) Next Page

Chapter 24. Accessibility

Subpages: 1. Java 2 Accessibility API overview
2. Following accessibility guidelines
3. Accessibility support for custom components

In this chapter:

  •          Java 2 Accessibility API overview
  •          Following accessibility guidelines
  •          Accessibility support for custom components

24.1  Java 2 Accessibility API overview

When developing an application, it is important to keep in mind that many of it's users may be disabled to some degree. Disabilities can range anywhere from poor vision to blindness, hearing impared to deafness, or a physical inability to use a standard input device such as a mouse or keyboard. Many of these users may often rely on special devices plugged into the computer to help offset specific disabilities. Some of these devices can substitute printed text with audio output, ease keyboard input or point-and-click functionality, perform large scale screen magnification, etc. To do their job correctly, most of these devices need to find connection points within applications, and substitute some of the original functionality with accessible variations.

Java offers sets of classes and interfaces to provide the needed connection points for such special devices. These are referred to as assistive technologies, and are normally packages in a JAR archive. Java loads assistive technologies when the VM is started by looking for special entries in a file called accessibility.properties located in the jdk1.2\jre\lib directory. We will discuss how to use this file below.

24.1.1  Implementing accessibile applications

Fortunately, Swing provides an extremely rich API for accessibility needs. If we use Swing "as is", there are only a few simple things we need to do in order to provide an accessible interface for assistive technologies:

Use tooltips to provide a text description of a component's purpose. The tooltip text may be used by accessible devices to provide a description of that component.

Enable keyboard access. Ideally, all functionality should be available without using the mouse. Use mnemonics for buttons and menus, set keyboard accelerators in menus.

Associate labels with components. By setting both the displayedMnemonic and labelFor properties, a label will transfer focus to the specified component with the labelFor property when the displayedMnemonic is activated (i.e. when ALT + mnemonic character is pressed).

Logically group components with labels refering to each when necessary.

Throughout this book we have adhered to most of these rules without explicitly being aware that we were also building accessible-compliant applications. Let's review a bit more about the association of labels with components. Consider the following code:

    JPanel p = new JPanel();

    p.add(new JLabel("IP address:"));

    JTextField user = new JTextField();


To make this code accessible we would need to associate the label with the text field above as follows:

    JPanel p = new JPanel();

    JLabel lbl = new JLabel("IP address:");



    JTextField user = new JTextField();

    user.setToolTipText("IP address");



The changes made should be familiar and are relatively simple. First, the setDisplayedMnemonic() method assigns a mnemonic character to the label component (this character will be underlined in the label if the label's text contains that character). Second, a tooltip text is assigned to the text field to provide an accessible text description. Finally, the setLabelFor() method is called to associate the text field component with the label. When the mnemonic is activated, the label will notify the text field to request the focus.

Note: As we've mentioned in chapter 19, due to a Swing bug or oversite, a mnemonic character will be passed to the text field as input. We will not concentrate on a workaround in this chapter, assuming that it will be fixed in future Swing releases.

Reference: To find out more about writing accessible Java applications, we refer you to IBM's guidelines at http://www.austin.ibm.com/sns/access.html.

Java 2 ships with the javax.accessibility package, which includes classes and interfaces constituting the Java Accessibility API. In the remainder of this section we'll give a brief introduction to some of its most important constituents.

Note: You may not need to explicitly use this package in your Swing application. Almost all of Swing has accessibile functionaliy built into it.

24.1.3  The Accessible interface

abstract interface javax.accessibility.Accessible

This interface must be implemented by all Java classes designed to support accessibility. All Swing components implement this interface either directly or indirectly. It declares only one method,  getAccessibleContext(), which returns an instance of AccessibleContext (see below).

24.1.4  AccessibleContext

abstract class javax.accessibility.AccessibleContext

This class provides the minimum information all accessible objects should expose. This information includes the accessible: name, description, role, and state of the object, as well as information about the parent and children of the object. To obtain more specific accessibility information about a component this class exposes methods to retrieve the  instances of interfaces defined in the accessibility package (see below).

Typically Swing components define an inner class which extends AccessibleContext and provides information specific to that component. For instance, JComponent provides the inner class AccessibleJComponent, JComboBox provides AccessibleJComboBox etc.

24.1.5  AccessibleRole

class javax.accessibility.AccessibleRole

This class defines several static constants used in providing information about the role a particular component plays in an application. Normally the getAccessibleRole() method of AccessibleContext will return one of these constants.

24.1.6  The AccessibleAction interface

abstract interface javax.accessibility.AccessibleAction

This interface declares methods to perform one or more accessible actions, retrieve descriptions of these actions, and find out exactly how many actions have been exposed to assistive technologies.

int getAccessibleActionCount(): returns the number of actions exposed through the doAccessibleAction() method.

String getAccessibleActionDescription(int i): should be defined to return a description of the code executed by doAccessibleAction() based on the given index.

boolean doAccessibleAction(int i): When implementing this interface we are expected to define code to be executed based on a given index. Normally this is involves invoking event handling code defined for a specific component, however, there is nothing stopping us from defining accessible-specific code here. It should return a boolean value representing whether or not the given index corresponds to a valid action.

24.1.7  The AccessibleComponent interface

abstract interface javax.accessibility.AccessibleComponent

This interface should be supported by any accessible component that is rendered to a graphical context. It provides the standard mechanism for an assistive technology to determine and assign certain aspects of the graphical representation of that component. Most methods will call the corresponding component's methods where applicable. (Note that no painting methods are members of this interface because we are expected to define our own look-and-feel if a more customized view is desired.)

24.1.8  The AccessibleHypertext interface

abstract interface javax.accessibility.AccessibleHypertext

This interface provides standard mechanisms for determining and manipulating hyperlinks with an assistive technology. Normally only text components will provide implementations of this interface, however, any other component supporting hypertext may do the same.

24.1.9  The AccessibleSelection interface

abstract interface javax.accessibility.AccessibleSelection

This interface should be implemented by any accessible component that has selectable children, whether individually or in groups, such as trees, tables, lists, tabbed panes, menu bars, etc.

24.1.10            The AccessibleText interface

abstract interface javax.accessibility.AccessibleText

This interface should be implemented by any accessible text component, and is intended to provide access to that component's document content and attributes (where applicable).

24.1.11            The AccessibleValue interface

abstract interface javax.accessibility.AccessibleValue

This interface should be implemented by any accessible component that supports a numerical value (e.g. a scroll bar). It is intended to be used by an assistive technology to determine and assign current, minimum, and maximum values.

24.1.12            Accessibility Utilities

In order to provide accessibility compliance between an application and an assistive technology, we cannot just rely on our use of the accessibility package. We also require support for locating the objects that implement accessibility functionality, as well as support for loading assistive technologies into the Java virtual machine and tracking all events sent to the system event queue. For these reasons, Sun provides the Java Accessibility Utilities which can be downloaded from the JavaSoft web site. Along with utility classes to perform these services, a set of sample applications is included which can be used to test and debug accessible Java programs. These applications are capable of linking with the system event queue and displaying information about selected types of events.

The Java Accessibility Utilities package is an assistive technology itself. To install it we must place the jaccess.jar and jaccess-examples.jar files into our jdk1.2\jre\lib\ext directory.

To use one of its assistive-technology examples, we need to include a string such as the following in our accessibility.properties file:


This will cause the Explorer utility to start each time we start a new JVM. (Note that if this file does not already exits we must create it ourselves.) In the examples below we assume that you have enabled the Explorer utility, and it is shown in the corresponding example figures.

Reference: A detailed description of the Java Accessibility Utilities lies beyond the scope of this book. Please see http://java.sun.com/products/jfc/jaccess-1.2/doc/guide.html for more information.

Note: Accessibility Utilities can be used by any Java application to monitor the content of the system event queue for debugging purposes.

[ Return to Swing (Book) ]

Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color

[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3

[ More in News Section ]

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy