Java – How to add a spell checker to Swing GUI text components (JOrtho / Wikitionary)

Screen Shot 2014-11-10 at 17.16.04

This tutorial uses JOrtho, a Java based spell checker, to implement a spell checker in a Swing based application.

We are going to use an English dictionary throughout this guide, though other languages are available by downloading your language dictionary from http://www.wiktionary.org/

JOrtho (Java Orthography) is an Open Source spell-checker entirely written in Java. Its dictionaries are based on the free Wiktionary project and can therefore be updated for virtually any language. The library works with any JTextComponent from the Swing framework. This includes JTextPane, JEditorPane and JTextArea.^

 

1) Firstly, get a copy of the JOrtho libraries available here.

Once you have a copy of the zip file you’ll extract the contents and find a file named jortho.jar.

Add the jar file to your classpath or project; In Netbeans, for example, right-click your project and select Properties. Select Libraries from the Categories context menu and hit Add JAR/Folder. Navigate to the folder containing jortho.jar, select the file and click Open.

Screen Shot 2014-11-10 at 16.25.50 Screen Shot 2014-11-10 at 16.26.03

Once successfully completed you’ll see jortho.jar under the libraries node in the object explorer.

 

2) Download/Create the dictionary file for your desired language.

You can create & compile your own dictionary from data available at http://www.wiktionary.org/ with instructions found here.

Or you can simply click here to download the English dictionary I last compiled on 10/11/14:

Total Wiktionary Title count: 3,607,810
Language Title count: 464,027
Word count in dictionary: 686,426
Char count in dictionary: 6,791,818
Dictionary size on disk (bytes): 1,914,453
Dictionary size in memory (bytes): 6,058,786

 

Create a folder named dictionary within the src folder of your application (src/dictionary/) and copy in two files from the downloaded zip:

  • dictionary_en.ortho
  • dictionaries.cnf

Screen Shot 2014-11-10 at 16.27.43

Dictionaries.cnf will open in a text editor where you’ll see a comma-separated list of supported languages. Remove any languages not supported in your project.

We will change the following line: languages=de,en,it,fr,es,ru
to: languages=en

Screen Shot 2014-11-10 at 17.24.48

 

3) We can now head over to your development environment and begin coding the spell checker.

Well assume you’ve already started your project, your GUI is built and that it contains one or more text input elements on a form.

Edit your code to include the following two import statements at the head of the document:

import com.inet.jortho.SpellChecker;
import com.inet.jortho.FileUserDictionary;

Add the following lines of code to initialise and register the dictionary:

//FILE LOCATION OF DICTIONARY
String userDictionaryPath = “/dictionary/”;

//SET DICTIONARY PROVIDER FROM DICTIONARY PATH
SpellChecker.setUserDictionaryProvider(new FileUserDictionary(userDictionaryPath));

//REGISTER DICTIONARY
SpellChecker.registerDictionaries(getClass().getResource(userDictionaryPath), “en”);

Finally, register the Swing text components with the spell checker. You can register as many components as required by your application by substituting the placeholder field names with the name of your components:

SpellChecker.register(jTextField1);
SpellChecker.register(jTextArea1);
SpellChecker.register(jTextPane1);

We’ll clean things up by placing the spell checker initialisation code within a method, which we’ll call on application start up:

Screen Shot 2014-11-10 at 17.29.00

 


4) Compile & run your application!

That’s all you need to include the JOrtho spell checker in your Java Swing application. Run your app and test the theory by typing a mixture of correctly & incorrectly spelt word in the input form. Misspelt words are highlighted by a red zig-zag line underneath. Right-click on the word to reveal the pop-up suggestion list.

Screen Shot 2014-11-10 at 17.15.54

Screen Shot 2014-11-10 at 17.16.04

Screen Shot 2014-11-10 at 17.16.09

 


5) Customising the suggestion pop-up

We’re supporting only the English language in our app so we have no need to display the language selector in our pop-up menu.

I’d also like to ignore case, set a limit of 10 alternative suggestions, ignore ALL CAPS WORDS & ignore words with numb3rs – all configurable with a custom pop-up menu.

Add three more imports into your code:

import com.inet.jortho.SpellCheckerOptions;
import javax.swing.JPopupMenu;
import com.inet.jortho.PopupListener; for options when got to that part.

Initialise a SpellCheckerOtions object:

SpellCheckerOptions sco = new SpellCheckerOptions();

Configure the options as required by your application:

sco.setCaseSensitive(false);
sco.setSuggestionsLimitMenu(10);
sco.setLanguageDisableVisible(false);
sco.setIgnoreAllCapsWords(true);
sco.setIgnoreWordsWithNumbers(true);
JPopupMenu popup = SpellChecker.createCheckerPopup(sco);

Finally, add the customised pop-up menu to the components as required:

jTextField1.addMouseListener(new PopupListener(popup));

Once again, we’ll clean things up by placing the SpellCheckerOtions initialisation code within a method, which we’ll call right after initialiseSpellChecker():

Screen Shot 2014-11-10 at 17.54.34

 

You’ll notice the pop-up menu go straight into the suggestions list with the customisations described above:

Screen Shot 2014-11-10 at 17.58.08
LINKS
JOrtho 1.0 (Direct Download): http://bit.ly/1uXO7fx
English dictionary compiled – check file for creation date (Direct Download): http://bit.ly/10QlcN3
Wikitionary.org: http://www.wiktionary.org/
JOrtho: http://jortho.sourceforge.net/

^Source: http://jortho.sourceforge.net/