How to make a red zig zag under word in JEditorPane

邮差的信 提交于 2019-11-29 23:47:14

问题


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.

How do I make a word in JEditorPane get underlined with a red zig-zag? Like this:

Please if any one has any simple and fairly easy to implement ideas please would you share them with me. Maybe even some links to some code.


回答1:


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.




回答2:


You can use a custom Highlighter. See Squiggle Painter as an example.




回答3:


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.



来源:https://stackoverflow.com/questions/10144815/how-to-make-a-red-zig-zag-under-word-in-jeditorpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!