Java – How to add a spell checker to Swing GUI text components JOrtho Library and Wikitionary Dictionaries

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.

 

lib1 lib2

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.

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

dic

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

dicCNF

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:

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

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

1
2
3
4
5
6
//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:

1
2
3
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.

4b 4a 4

 

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:

1
2
3
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:

1
SpellCheckerOptions sco = new SpellCheckerOptions();

Configure the options as required by your application:

1
2
3
4
5
6
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:

1
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():

5

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

5a

 

LINKS

Wikitionary.org: http://www.wiktionary.org/
JOrtho: http://jortho.sourceforge.net/

 

1 thought on “Java – How to add a spell checker to Swing GUI text components JOrtho Library and Wikitionary Dictionaries”

  1. Very good explanations. Most sites that explain the same subject assume things that are not explicitly written (for example how to create the dictionaries.cnf). Well done!

You can say something here...