O'Reilly logo

Mac OS X for Java Geeks by Will Iverson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required