Skip to Page Content | Navigation for Module


Navigation for Module 10: Scripts/Java
Page 15 of 19

Image Icons in Java

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


Image 6: JButton example with an icon

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.

Code example sets description of icon that appears on a button:

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.

Top of Page arrow up
       Page 15


 
-- END OF PAGE