Java by API/javax.accessibility/AccessibleRelationSet

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

AccessibleRelationSet: get(String key)

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());
  }
}