Java by API/javax.accessibility/AccessibleContext

Материал из Java эксперт
Перейти к: навигация, поиск

AccessibleAction: getAccessibleActionCount()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleContext;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class MainClass extends JFrame {
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToolBar toolBar = new JToolBar();
    Action a = new AbstractAction("Demo") {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Action taken.");
      }
    };
    JButton b = toolBar.add(a);
    b.setText("Demo Button");
    b.setToolTipText("Press me to take action.");
    JMenu mainMenu = new JMenu("Menu");
    JMenuItem mi = mainMenu.add(a);
    mi.getAccessibleContext().setAccessibleName("Menu item");
    JMenuBar mb = new JMenuBar();
    mb.add(mainMenu);
    setJMenuBar(mb);
    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.setPreferredSize(new Dimension(200, 100));
    pane.add(toolBar, BorderLayout.NORTH);
    setContentPane(pane);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad2;
    ad2 = new MainClass("Accessibility Demo2");
    ad2.dumpActionInfo(ad2.getAccessibleContext());
  }
  void dumpActionInfo(AccessibleContext ac) {
    AccessibleAction aa = ac.getAccessibleAction();
    if (aa != null) {
      String s = ac.getAccessibleName();
      System.out.println(s);
      int count = aa.getAccessibleActionCount();
      for (int i = 0; i < count; i++) {
        s = aa.getAccessibleActionDescription(i);
        System.out.println("Description = " + s);
      }
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpActionInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: getAccessibleAction()

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.accessibility.AccessibleAction;
import javax.accessibility.AccessibleContext;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
public class MainClass extends JFrame {
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToolBar toolBar = new JToolBar();
    Action a = new AbstractAction("Demo") {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Action taken.");
      }
    };
    JButton b = toolBar.add(a);
    b.setText("Demo Button");
    b.setToolTipText("Press me to take action.");
    JMenu mainMenu = new JMenu("Menu");
    JMenuItem mi = mainMenu.add(a);
    mi.getAccessibleContext().setAccessibleName("Menu item");
    JMenuBar mb = new JMenuBar();
    mb.add(mainMenu);
    setJMenuBar(mb);
    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.setPreferredSize(new Dimension(200, 100));
    pane.add(toolBar, BorderLayout.NORTH);
    setContentPane(pane);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad2;
    ad2 = new MainClass("Accessibility Demo2");
    ad2.dumpActionInfo(ad2.getAccessibleContext());
  }
  void dumpActionInfo(AccessibleContext ac) {
    AccessibleAction aa = ac.getAccessibleAction();
    if (aa != null) {
      String s = ac.getAccessibleName();
      System.out.println(s);
      int count = aa.getAccessibleActionCount();
      for (int i = 0; i < count; i++) {
        s = aa.getAccessibleActionDescription(i);
        System.out.println("Description = " + s);
      }
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpActionInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: getAccessibleChildrenCount()

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}





AccessibleContext: getAccessibleDescription()

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}





AccessibleContext: getAccessibleName()

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}





AccessibleContext: getAccessibleRelationSet()

import java.awt.Dimension;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRelation;
import javax.accessibility.AccessibleRelationSet;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainClass extends JFrame {
  
  MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(300, 50));
    JLabel jl = new JLabel("Name:");
    p.add(jl);
    JTextField jtf = new JTextField(20);
    jtf.getAccessibleContext().setAccessibleName("Name-entry");
    p.add(jtf);
    AccessibleRelation ar = new AccessibleRelation("connector", jtf);
    AccessibleContext ac = jl.getAccessibleContext();
    ac.getAccessibleRelationSet().add(ar);
    getContentPane().add(p);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad11 = new MainClass();
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    ad11.dumpConnectedInfo(ad11.getAccessibleContext());
  }
  void dumpConnectedInfo(AccessibleContext ac) {
    AccessibleRelationSet ars = ac.getAccessibleRelationSet();
    AccessibleRelation ar = null;
    if (ars != null)
      ar = ars.get("connector");
    if (ar != null) {
      Object[] o = ar.getTarget();
      JComponent jc = (JComponent) o[0];
      System.out.println("Label connected to: " + jc.getAccessibleContext().getAccessibleName());
      return;
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpConnectedInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: getAccessibleSelection()

import java.awt.Dimension;
import java.util.Vector;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleSelection;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MainClass extends JFrame {
  MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector v = new Vector();
    v.add("First item");
    v.add("Second item");
    v.add("Third item");
    v.add("Fourth item");
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(200, 100));
    JList jl = new JList(v);
    jl.setPreferredSize(new Dimension(100, 75));
    p.add(new JScrollPane(jl));
    getContentPane().add(p);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad6 = new MainClass();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    ad6.dumpSelectionInfo(ad6.getAccessibleContext());
  }
  void dumpSelectionInfo(AccessibleContext ac) {
    AccessibleSelection as = ac.getAccessibleSelection();
    if (as != null) {
      int count = as.getAccessibleSelectionCount();
      for (int i = 0; i < count; i++) {
        Accessible a = as.getAccessibleSelection(i);
        AccessibleContext ac2 = a.getAccessibleContext();
        String s = ac2.getAccessibleName();
        System.out.println("Name = " + s);
      }
      return;
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpSelectionInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: getAccessibleTable()

import java.awt.Dimension;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleTable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MainClass extends JFrame {
  MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(200, 200));
    JTable jt = new JTable(10, 3);
    p.add(new JScrollPane(jt));
    getContentPane().add(p);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad7 = new MainClass();
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    ad7.dumpTableInfo(ad7.getAccessibleContext());
  }
  void dumpTableInfo(AccessibleContext ac) {
    AccessibleTable at = ac.getAccessibleTable();
    if (at != null) {
      int nCol = at.getAccessibleColumnCount();
      System.out.println("Columns = " + nCol);
      int nRow = at.getAccessibleRowCount();
      System.out.println("Rows = " + nRow);
      return;
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpTableInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: getAccessibleText()

import java.awt.Dimension;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleText;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MainClass extends JFrame {
  MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(300, 50));
    JTextField jtf = new JTextField(20);
    jtf.setPreferredSize(new Dimension(100, 20));
    p.add(jtf);
    getContentPane().add(p);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad8 = new MainClass();
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    ad8.dumpTextInfo(ad8.getAccessibleContext());
  }
  void dumpTextInfo(AccessibleContext ac) {
    AccessibleText at = ac.getAccessibleText();
    if (at != null) {
      System.out.println("Caret position = " + at.getCaretPosition());
      System.out.println("Selected text = " + at.getSelectedText());
      return;
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpTextInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: getAccessibleValue()

import java.awt.Adjustable;
import java.awt.Dimension;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleValue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
public class MainClass extends JFrame {
  MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(300, 50));
    JScrollBar jsb;
    jsb = new JScrollBar(Adjustable.HORIZONTAL, 0, 1, 0, 100);
    jsb.setPreferredSize(new Dimension(200, 20));
    p.add(jsb);
    getContentPane().add(p);
    pack();
    setVisible(true);
  }
  public static void main(String[] args) {
    MainClass ad9 = new MainClass();
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    ad9.dumpValueInfo(ad9.getAccessibleContext());
  }
  void dumpValueInfo(AccessibleContext ac) {
    AccessibleValue av = ac.getAccessibleValue();
    if (av != null) {
      System.out.println("Minimum value = " + av.getMinimumAccessibleValue());
      System.out.println("Current value = " + av.getCurrentAccessibleValue());
      System.out.println("Maximum value = " + av.getMaximumAccessibleValue());
      return;
    }
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpValueInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
}





AccessibleContext: setAccessibleDescription(String s)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}





AccessibleContext: setAccessibleName(String s)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}