macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Adding Spell Checking to Your Swing Applications
Pages: 1, 2

Adding Real-time Checking

To add real-time checking, you just need to invoke the startRealtimeChecking() method of the JTestComponentDriver when the JCheckBox is checked, and stopRealtimeChecking() when it is unchecked. To accomplish this, add the line:



realTimeSpellCheckingCheckBox.addActionListener(new RealTimeSpellingListener());

to the body of the createActionListeners() method, and add the following inner class to SpellingToolbar.


class RealTimeSpellingListener implements ActionListener{

    public void actionPerformed(ActionEvent e)
    {
      if (realTimeSpellCheckingCheckBox.isSelected()) {
        driver.startRealtimeChecking(textComponent);
      } else {
        driver.stopRealtimeChecking();
      }
    }
  }

That's it. Save, recompile, and run. This time, type in the same phrase we typed above and then check the Real Time Checking checkbox and continue typing. This time the spell checker does nothing to check the words you typed before checking the box. It does, however, give you immediate feedback on what you're typing. This should feel familiar to you, as you've seen this behavior in Mail and other Mac OS X applications. The red underline disappears when you go back and correct a misspelling. Here's a sample session where I've checked the checkbox after typing the word "words."

Session with checked box

You can see that the word "evening" has been mistakenly marked, but the spell checker is still learning.

Getting Suggestions

At this point, it would be nice if we could add the word evening to the dictionary or get suggestions for some of the identified words. Again, this amounts to adding another line to the createActionListeners() method along with another inner class. The entire code listing (such as it is) for SpellingToolbar is at the end of this section. Add the line:

enterSpellingSessionButton.addActionListener(new SpellingSessionListener());

to the body of createActionListeners(). Add the following inner class to SpellingToolbar.


class SpellingSessionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      driver.checkSpelling(textComponent);
    }
  }

Save, compile, and run your application. Type in the same text as before with Real Time Checking on. Select the "Get Suggestions" button and you'll see this familiar dialog box.

Spelling correction window

Notice that the misspelled word is highlighted with some guesses for the intended word. You can select one, type in another, choose to ignore this word, or add or delete a word from the dictionary. If you press the "Correct" button, your corrections to the spelling of the word will replace the highlighted word in the text. I've already used this technique to replace "exzample" with "example."

Here is the entire source code for SpellingToolbar.java:


import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.text.JTextComponent;
import com.apple.spell.ui.JTextComponentDriver;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class SpellingToolbar extends JPanel {

  private JTextComponentDriver driver = new JTextComponentDriver();
  private JTextComponent textComponent;
  private JCheckBox realTimeSpellCheckingCheckBox;
  private JButton markMisspelledWordsButton, enterSpellingSessionButton;

  public SpellingToolbar(JTextPane textPane){

   textComponent = textPane;
   createButtons();
   createActionListeners();
   addButtonsToPanel();

  }

  private void createActionListeners() {
    markMisspelledWordsButton.addActionListener(new MisspelledWordsMarkerListener());
    realTimeSpellCheckingCheckBox.addActionListener(new RealTimeSpellingListener());
    enterSpellingSessionButton.addActionListener(new SpellingSessionListener());
  }

  private void createButtons() {
    markMisspelledWordsButton = new JButton("Mark Misspelled Words");
    realTimeSpellCheckingCheckBox = new JCheckBox("Real Time Checking", false);
    enterSpellingSessionButton = new JButton("Get Suggestions");
  }

  private void addButtonsToPanel() {
    add(markMisspelledWordsButton);
    add(realTimeSpellCheckingCheckBox);
    add(enterSpellingSessionButton);
  }

   class MisspelledWordsMarkerListener implements ActionListener{

      public void actionPerformed(ActionEvent e) {
        driver.markMisspelledWords(textComponent,0,-1);
    }
  }

  class RealTimeSpellingListener implements ActionListener{

    public void actionPerformed(ActionEvent e) {
      if (realTimeSpellCheckingCheckBox.isSelected()) {
        driver.startRealtimeChecking(textComponent);
      } else {
        driver.stopRealtimeChecking();
      }
    }
  }


  class SpellingSessionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      driver.checkSpelling(textComponent);
    }
  }

}

That's a quick introduction to spell checking your Java application on Mac OS X. I know I promised not to look ahead to future columns, but I will return later this year to look at the Speech frameworks. It seems there's an event related to Java on the Mac all the time. MacWorld in January, QuickTime Live in February, JavaOne in March... I'm sure there'll be plenty to discuss.