JAVA ACCESSIBILITY
Getting text letter-by-letter
Version 0.5
08 October 1997
[Home]
[What is it?] [Overview
of API Features] [API
Reference]
[FAQ] [Tutorial]
[Tools] [Changes]
[JFC Home Page]
Getting text letter-by-letter
Getting text letter-by-letter, while keeping track of the pixel location
of the letter boundaries, is done via the AccessibleText
interface. First retrieve an instance of AccessibleText
by calling Accessible.getAccessibleText
on an object that implements interface
Accessible; then either start at a particular index location into the
text, or get an index location from screen coordinates using AccessibleText.getIndexAtPoint;
and finally, use AccessibleText.getAtIndex,
AccessibleText.getAfterIndex,
and AccessibleText.getBeforeIndex
to find the letters/words/sentences at that location.
Below are several code examples illustrating how to use these interfaces:
Get the letter under the mouse
In order to get the letter under the mouse, we first need to get the accessible
object under the mouse, if there is one. Once we have that, we query
it to get it's AccessibleText,
if it has one. Once we have that, we get the index at the mouse coordinate
(translated to local coordinates), and then the letter at that index location.
In Java code, it looks like this:
Point currentMousePos = EventQueueMonitor.getCurrentMousePosition();
Accessible accessible = EventQueueMonitor.getAccessibleAt(currentMousePos);
if (accessible != null) {
AccessibleText text = accessible.getAccessibleText();
if (text != null) {
Point containerLoc =
accessible.getLocationOnScreen();
Point containerPoint
= new Point(currentMousePos.x - containerLoc.x,
currentMousePos.y - containerLoc.y);
int index = text.getIndexAtPoint(containerPoint);
String letter = text.getAtIndex(AccessibleText.CHARACTER,
index);
// do something interesting
here with the letter
}
}
Get the bounding rectangle of the first
visible letter showing in the text component
In order to get the bounding rectangle of the first letter showing in the
component, we need both the component itself, from which we get geometry
information, and that component's AccessibleText.
Once we have those, we get the index at the mouse coordinate of the top-left
corner of the component, and then the bounding recangle at that index location.
The code sample below assumes we get the text component by finding it underneath
the mouse; but we could get it via some other technique as well.
In Java code, it looks like this:
Point currentMousePos = EventQueueMonitor.getCurrentMousePosition();
Accessible accessible = EventQueueMonitor.getAccessibleAt(currentMousePos);
if (accessible != null) {
AccessibleText text = accessible.getAccessibleText();
if (text != null) {
Point p = new Point(0,0);
int index = text.getIndexAtPoint(p);
Rectangle bounds = text.getCharacterBounds(index);
// do something interesting
here with the rectangle
}
}
Get the entire text contents of the component,
one letter at a time
To get all of the text of a component, one letter at a time, we need to
get the compnent's AccessibleText,
and then loop through that text, starting at the first index, through the
end. While this technique is not something one might normally use
(there are more effecient ways to get all of the text of a component),
it illustrates how to use the AccessibleText
methods. The code sample below assumes we get the text component
by finding it underneath the mouse; but we could get it via some other
technique as well.
In Java code, it looks like this:
Point currentMousePos = EventQueueMonitor.getCurrentMousePosition();
Accessible accessible = EventQueueMonitor.getAccessibleAt(currentMousePos);
if (accessible != null) {
AccessibleText text = accessible.getAccessibleText();
if (text != null) {
int charLength = text.getCharCount();
String letter;
for (int index = 0;
index < charLength; index++) {
letter = text.getAtIndex(AccessibleText.CHARACTER, index);
// do something interesting here with the letter
}
}
Copyright©
1995-97 Sun Microsystems, Inc. All Rights
Reserved.
To submit comments or suggestions about Java
Accessibility, please send mail to access@sun.com.