Wintertree Software Inc.

Sentry Spell Checker for Java

Home Site index Contact us Catalog Shopping Cart Products Support Search

You are here: Home > Add a spell checker to your applications > Java


Add a spell checker to your Java applications, applets, and servlets with Sentry Spell Checker

Sentry Spell Checker provides everything you need to add a full spell check capability to your Java applications and applets on any platform. Click here to find out more about Sentry Spell Checker.

Click on a topic below to find out more about how Sentry Spell Checker works with your Java applications:


How to spell check a JTextArea or JTextPane component

Java spell check dialog

Many Java applications let their users enter text via a Swing JTextArea or JTextPane component. If this is how your application works, you can add a spell checker in minutes using these simple steps:

  1. Add a "Spell check" button or menu item to the form or dialog containing the JTextArea or JTextPane component.

  2. Construct a SpellingSession object. The SpellingSession object is your application's interface to the Sentry API.
  3. Construct a JTextComponentWordParser object. This object is used by JSpellCheckDlg to extract and replace words in the text area component.
  4. In the event handler for your "Spell check" button or menu item, construct a JSpellCheckDlg object, passing the JTextComponentWordParser object.
  5. Call the JSpellCheckDlg object's setVisible method to start the spell check operation.

That's all there is to it! During the spelling check, the JSpellCheckDlg object interacts with the user to check the spelling of the text. If the user corrects a misspelled word, the text is automatically updated. When the spelling check is complete, the JTextArea or JTextPane component will contain the corrected text. Any style attributes set in JTextPane (e.g., font size, bold, etc.) will be preserved when the user makes corrections.


How to spellcheck a JTextArea or JTextPane component in the background

Many modern word processors support background (also known as "as-you-type," passive, or "on-the-fly") spell-checking, where misspelled words are highlighted by underlining with a red zigzag line. With Sentry Java SDK's BackgroundChecker class, you can add this feature in minutes:

  1. Construct a SpellingSession object. The SpellingSession object is your application's interface to the Sentry API.

  2. Construct a BackgroundChecker object, and pass a reference to your JTextArea or JTextPane to the constructor.

That's it! BackgroundChecker will listen for events from the JTextArea or JTextPane component. When it detects a misspelled word, it will highlight the word with a red zigzag underline. If the user corrects the misspelled word by editing, BackgroundChecker will remove the zigzag as soon as the user's changes correct the word.

If you'd like to display a popup menu when the user right-clicks over a misspelled word (on Windows; other platforms use different popup triggers), just do the following:

  1. Implement the MouseListener interface in your application's class.

  2. In response to the mouseClicked, mouseEntered, mouseExited, mousePressed, and mouseReleased notifications, call the isPopupTrigger on the MouseEvent object to determine if the popup menu was requested. The specific mouse events that cause isPopupTrigger to return true are platform dependent; on Windows, isPopupTrigger returns true when the right mouse button is pressed.

  3. If the popup menu is requested, call BackgroundChecker's isInMisspelledWord method to determine if the popup menu was requested over a word highlighted as misspelled.

  4. If the popup menu was requested over a misspelled word, call BackgroundChecker's createPopupMenu method to create a JPopupMenu object populated with items specific to the misspelled word, including suggested replacements and, optionally, Ignore All and Add items.

  5. Call JPopupMenu's show method to present the popup menu. If the user selects a suggested replacement, the misspelled word in the JTextArea or JTextPane will be corrected.


How to spell check Strings

The text you want to spell check may not be entered by the user -- it may be read from a disk file or database, or obtained from data structures within your Java application.

Sentry Spellchecker can check words contained in Java Strings interactively or not interactively.

To check a String interactively:

  1. Create a StringWordParser (part of the Sentry class library), and pass the String to the constructor. The String can contain a single word or an entire book.

  2. Create a SpellingDialog (AWT) or JSpellCheckDlg (Swing) object and pass the StringWordParser to the constructor
  3. Call the SpellingDialog's or JSpellCheckDlg's setVisible method to being the spell check operation.

Sentry will check the spelling of words in the String. If a misspelled word is found, Sentry will pop up a dialog the user can interact with to dispose of the problem. The dialog shows suggested replacements for the misspelled word. If the user makes a correction, the String will be updated automatically. Sentry can even optionally show the String's contents in a text box attached to the dialog so the user can see the misspelled words in context.

Checking strings non-interactively is done via the check method of the SpellingSession object. The check method checks the form and spelling of each word in turn, stopping when it finds a word which requires the attention of your application or it reaches the end of the string. Your Java application should call the check method in a loop; the loop terminates when the check method indicates that the end of the string has been reached. If the check method returns because it has encountered a problem with a word (e.g., a misspelling), your application should respond to the problem (e.g., by replacing the misspelled word in the string with a correctly spelled word), then continuing with the loop in which the check method is called. Your application can obtain a list of suggested replacements for the misspelled word from the Suggest function.


Home Site index Contact us Catalog Shopping Cart Products Support Search


Copyright © 2015 Wintertree Software Inc.