Spelling in Action

Now that you have a basic understanding of the Spelling API, consider the actual code that uses it. As in previous chapters, this code should address any questions you might have and detail the use of spelling from a Java application.

User-Requested Spellchecking

You’ll now add the ability to spellcheck the JTextArea of the SimpleEdit application created in Chapter 4. This modification allows the user to select “Spelling” from the “Tools” menu and run a spellcheck on SimpleEdit application text.

The plug-in mechanism adds this functionality, implementing the SimpleEditPlugin interface again. This makes interaction with SimpleEdit a piece of cake. Example 11-1 is the source code for this plug-in.

Example 11-1. A spellchecking plug-in

package com.wiverson.macosbook.spelling;

import com.wiverson.macosbook.SimpleEdit;

public class SpellCheckPlugin implements 
        com.wiverson.macosbook.SimpleEditPlugin
{
    
    public SpellCheckPlugin(  )
    {
    }
    
    public void doAction(SimpleEdit frame, java.awt.event.ActionEvent evt)
    {
        com.apple.spell.ui.JTxtCmpontDrvr mySpellchecker = 
        new com.apple.spell.ui.JTxtCmpontDrvr(  );
        mySpellchecker.checkSpelling(frame.getJTextArea(  ));
    }
    
    public String getAction(  )
    {
        return "Check Spelling...";
    }
    
    public void init(SimpleEdit frame)
    {
    }
    
}

There’s very little to this code; no initialization is required, so only the doAction( ) method and a short getAction( ) method body need to be implemented. getAction( ) is self-explanatory, and doAction( ) just loads a text-area ...

Get Mac OS X for Java Geeks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.