I need to make a simple spell checker in Java for an application that I am creating and I have searched around and have not found any straight and to the point answers.
You can use a custom Highlighter. See Squiggle Painter as an example.
You may find this custom editor kit example useful. It shows how to extend StyledEditorKit
to add attribute to draw custom underline.
If you're up for a complete solution you can go with Jide's StyledLabel. Check it out here. It should be part of jide-oss
- common open source library.
I use jtstand's editor and SquiggleUnderlineHighlightPainter, which is also based on javax classes. I use it like this:
JTextComponent editor = //... your editor component;
SquiggleUnderlineHighlightPainter sqpainter = new SquiggleUnderlineHighlightPainter(Color.RED);
try {
editor.getHighlighter().addHighlight(beginPosition, endPosition, sqpainter);
}
catch (BadLocationException e) {
e.printStackTrace();
}
This should work with any javax.swing.text.JTextComponent
, including JEditorPane
. See addHighlighter.