An example of getting the Accessible information from a Button object : Accessible « Swing JFC « Java

An example of getting the Accessible information from a Button object

An example of getting the Accessible information from a Button object

Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
//An example of getting the Accessible information from a Button object.
//(You could see this information by attaching an AssistiveExample object
//to the button. See for an example of that attachment.)

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.accessibility.AccessibleStateSet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ActionExample extends Button implements ActionListener, Accessible {

  public ActionExample() {
    super("Press this Button");

  public AccessibleContext getAccessibleContext() {
    return (new ActionAccessibleContext());

  public void actionPerformed(ActionEvent e) {
    System.out.println("The button was pressed!");

  public void processActionEvent(ActionEvent e) {

  // This class contains the accessible context for the component. Many
  // abstract methods simply call the SwingUtilities class to get the job
  // done; this is advised if you can get away with it. Otherwise, see the
  // source code for SwingUtilities.
  class ActionAccessibleContext extends AccessibleContext {

    public ActionAccessibleContext() {
      setAccessibleDescription("Press the Button");

    public AccessibleRole getAccessibleRole() {
      // Fill in whatever role you want here
      return (AccessibleRole.AWT_COMPONENT);

    public AccessibleStateSet getAccessibleStateSet() {
      return SwingUtilities.getAccessibleStateSet(ActionExample.this);

    public int getAccessibleIndexInParent() {
      return SwingUtilities

    public int getAccessibleChildrenCount() {
      return SwingUtilities

    public Accessible getAccessibleChild(int i) {
      return SwingUtilities.getAccessibleChild(ActionExample.this, i);

    public Locale getLocale() {
      //  Ask the component what its locale is
      return ActionExample.this.getLocale();

    public AccessibleAction getAccessibleAction() {
      return new AccessAction();

  // This class implements the AccessibleAction interface. Essentially, there
  // is only one action that is the equivalent of pushing the button.
  class AccessAction implements AccessibleAction {

    final int NUMBER_OF_ACTIONS = 1;

    final String DESCRIPTION = "Presses the button";

    public int getAccessibleActionCount() {
      return NUMBER_OF_ACTIONS;

    public String getAccessibleActionDescription(int i) {
      if (i == 0)
        return (DESCRIPTION);
        return null;

    public boolean doAccessibleAction(int i) {
      if (i == 0) {
        // Simulate pressing a button
        ActionExample.this.processActionEvent(new ActionEvent(this,
            ActionEvent.ACTION_PERFORMED, ActionExample.this
        return true;
      } else
        return false;

  public static void main(String s[]) {

    ActionExample example = new ActionExample();

    JFrame frame = new JFrame("AccessibleAction Example");
    frame.getContentPane().add(example, BorderLayout.CENTER);
    frame.setSize(100, 100);


Related examples in the same category

1.implement Accessible to make your object accessible
2.Setting an Accessible Name for an Image Button
3.Setting a Description for Image Icons
4.Setting a Mnemonic for Buttons for Accessible
5.Setting a Mnemonic for a Menu for Accessible
6.Associating a Label with a Component
7.Setting a Keyboard Accelerator for a Menu Item
8.A GUI to show accessible information coming from the components in an
9.Has Displayed Name