1

I am implementing a spellchecker and wondered if there is an easy/obvious way of underlining some text in a different colour such as Red.

I have everything set up and underlining with the following code (also sets the color of the text):

private AttributeSet getAttributeSet(Color foregroundColor) {
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setForeground(attrs, foregroundColor);
    StyleConstants.setUnderline(attrs, true);
}

The above code sets the word to blue but also underlines it in blue. I need to be able to change the underline and also its thickness. Any ideas?

Thanks,

mKorbel
  • 109,525
  • 20
  • 134
  • 319
maloney
  • 1,633
  • 3
  • 26
  • 49

1 Answers1

5

Use e.g this http://java-sl.com/tip_colored_strikethrough.html

Just modify it a bit to achieve underline rather than strikethrough.

But from my experience to highlight spellcheck errors it's better to define custom highlight painter

See also http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneHighlightExample.htm

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks for the reply. In the custom highlight painter, what goes into: paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) – maloney Mar 01 '12 at 09:29
  • +1 Thats useful ... just used it for underlines by making simple change to the y equation. – Sid Malani Aug 22 '12 at 12:11