JavaSoft - Sun MicroSystems
JAVA ACCESSIBILITY
Getting Text Word-by-Word
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 word-by-word

Getting text word-by-word, 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 word under the mouse

In order to get the word 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 word at that index location.

In Java code, it looks like this:

 

Get the the last visible word showing in the text component

In order to get the last word 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 word 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:

 

Get the entire text contents of the component, one word at a time

To get all of the text of a component, one word 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:

  
Java Logo Copyright© 1995-97 Sun Microsystems, Inc. All Rights Reserved.
To submit comments or suggestions about Java Accessibility, please send mail to access@sun.com.

Sun MicroSystems