HighlightPainter Demo

What Is It

This demo features two simple new HighlightPainters: the UnderlineHighlightPainter and the TextBoxHighlightPainter.

How To Use It

Swing already does all the heavy lifting here for us. We can add these just like any other HighlightPainter:

UnderlineHighlightPainter painter =
    new UnderlineHighlightPainter(Color.red, 1, true);
String word = "the";
int i1 = textPane.getText().indexOf(word);
if (i1 != -1) {
    int i2 = i1 + word.length();
    textPane.getHighlighter().addHighlight(i1, i2, painter);
}

Discussion

The "squiggly" underline has become the ubiquitous marker for cautionary flags in text panes. A decade or two ago it was only used to flag misspellings, but increasingly it's also used to flag grammatical warnings. You could also flag syntactical errors or warnings for a language like XML or Java.

The TextBoxHighlightPainter is a simpler upgrade to the DefaultHighlightPainter that offers a potential gradient or rounded corners.