JAVA ACCESSIBILITY
Getting Text Sentence-by-Sentence
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 sentence-by-sentence
Getting text sentence-by-sentence, 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 sentence under the mouse
In order to get the sentence 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 sentence 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.SENTENCE,
index);
// do something interesting
here with the letter
}
}
Get the the last visible sentence showing
in the text component
In order to get the last sentence completely 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 lower-right
corner of the component, and then the sentence before 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) {
Dimension d = accessible.getSize();
Point p = new Point(width,
height);
String sentence = text.getBeforeIndex(AccessibleText.SENTENCE,
index);
// do something interesting
here with the text
}
}
Get the entire text contents of the component,
one sentence at a time
To get all of the text of a component, one sentence 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. 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 sentence = text.getAtIndex(AccessibleText.SENTENCE,
0); ;
// do something interesting
here with the word
int index = sentence.length();
while (index < charLength)
{
sentence = text.getAfterIndex(AccessibleText.SENTENCE, index);
index += sentence.length();
// do something interesting here with the sentence
}
}
Copyright©
1995-97 Sun Microsystems, Inc. All Rights
Reserved.
To submit comments or suggestions about Java
Accessibility, please send mail to access@sun.com.