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 
*/
// ActionExampleButton.java
//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 BigExample.java 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");
    addActionListener(this);
  }

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

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

  public void processActionEvent(ActionEvent e) {
    super.processActionEvent(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() {
      super();
      setAccessibleName("Button");
      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
          .getAccessibleIndexInParent(ActionExample.this);
    }

    public int getAccessibleChildrenCount() {
      return SwingUtilities
          .getAccessibleChildrenCount(ActionExample.this);
    }

    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);
      else
        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
                .getActionCommand()));
        return true;
      } else
        return false;
    }
  }

  public static void main(String s[]) {

    ActionExample example = new ActionExample();

    JFrame frame = new JFrame("AccessibleAction Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(example, BorderLayout.CENTER);
    frame.setSize(100, 100);
    frame.setVisible(true);
  }
}

           
         
  








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