Now that you have been taught about mnemonics and accelerators, you can move on to image icons. Because JButtons have the potential to contain an associated image, and because any component that represents itself graphically should have a textual description, it follows that these images that appear on buttons should have a description, even if the button already has one (see Image 6).
Doing this requires an extra step beyond getting the accessible context. Using
the accessible context, it is possible to pull an array of accessible
icons from the component using the getAccessibleIcon( ) method. An array
makes it possible to associate multiple icons with an individual component.
Once you have your AccessibleIcon array, it is possible to set the description
using the setAccessibleIconDescription( ) method.
import javax.swing.*;
import javax.accessibility.*;
class AccessibleIconExample extends JFrame {
JButton button;
AccessibleIconExample() {
ImageIcon ii = new ImageIcon ("images/file");
button = new JButton ("Open File",
ii);
// Get the AccessibleContext
AccessibleContext ac = button.getAccessibleContext();
// Pull the array of icons out
AccessibleIcon[] ais = ac.getAccessibleIcon();
// Set the description of the first
image
ais[0].setAccessibleIconDescription
("File Cabinet");
this.getContentPane().add (button);
this.pack();
this.setVisible(true);
}
public static void main (String args[]) {
AccessibleIconExample aie = new
AccessibleIconExample();
}
}
The next page will discuss labels and tables.