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.
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, select the file and click Open.
Once successfully completed you’ll seeunder the libraries node in the object explorer.
2) Download/Create the dictionary file for your desired language.
Create a folder named dictionary within the src folder of your application (src/dictionary/) and copy in two files from the downloaded zip:
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
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:
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
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:
We’ll clean things up by placing the spell checker initialisation code within a method, which we’ll call on application start up:
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.
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.PopupListener; //for options when got to that part
Initialise a SpellCheckerOtions object:
SpellCheckerOptions sco = new SpellCheckerOptions();
Configure the options as required by your application:
JPopupMenu popup = SpellChecker.createCheckerPopup(sco);
Finally, add the customised pop-up menu to the components as required:
Once again, we’ll clean things up by placing the SpellCheckerOtions initialisation code within a method, which we’ll call right after initialiseSpellChecker():
You’ll notice the pop-up menu go straight into the suggestions list with the customisations described above: