11.2 The Spelling API

The spelling kit offers several different layers of API. The selection of an API depends on the customization you wish to offer and the type of application you are building.

The com.apple.spell.SpellChecker class is the fundamental building block for spelling. This class contains several static methods that work with ordinary Java Strings. It lets you search for misspelled words, which are then returned as com.apple.spell.MisspelledWord objects. You can work with multiple languages, ask for suggestions, and add and remove words from the dictionary?all without worrying about the details of the underlying spellchecking engine. These classes do not, however, provide any sort of user interface.

The next layer of the API calls the method com.apple.spell.ui.JTextComponentDriver.checkSpelling( ) , passing in a javax.swing.JTextComponent to the method. This step invokes a standard spelling dialog (as shown in Figure 11-5). Alternatively, you can call com.apple.spell.ui.JTextComponentDriver.startRealtimeChecking( ) on a JTextComponent to get real-time spellchecking (in which spelling mistakes are underlined in red as the user enters incorrect words).

The third layer of the API registers event handlers to receive notifications when the com.apple.spell.ui.JTextComponentDriver class processes events such as finding a misspelled word or changing, correcting, or ignoring a word.

This design is attractive because it lets Swing applications add support for spellchecking. At the same time, less conventional applications can rely on the same underlying functionality to perform spellcheck with whatever custom user interface is desired.