Giannis
Giannis

Reputation: 5526

Highlighting line of JTextArea

I am trying to highlight multiple lines of a JTextArea subclass (not selected). Although I want to highlight the whole line and not just the text each line contains. I got this working but it only highlights text :

DefaultHighlighter h = (DefaultHighlighter)textArea.getHighlighter(
    try {
            int start = textArea.getLineStartOffset(blockedLine);
            int end = textArea.getLineEndOffset(blockedLine);
            DefaultHighlightPainter redHighlight = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
            h.addHighlight(start, end, redHighlight);
        } catch (BadLocationException ex) {
            Logger.getLogger(JavaFilter.class.getName()).log(Level.SEVERE, null, ex);
        }

How can I highlight the whole line instead of just the text ? I am using an open source library for the text area so getting a JTextPane or other component is not an option (using rysntaxtextarea library).

Upvotes: 3

Views: 1463

Answers (2)

Evgeni Sergeev
Evgeni Sergeev

Reputation: 23593

The idea from @camickr's LinePainter.java applies here: implement Highlighter.HighlightPainter, getting its paint(..) method to go outside of the bounds that it is given, painting a rectangle of the same height and full width.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109815

I'd suggest to use JTextPane / JEditorPane, these JTextComponents can be decorated and supporting styled Text, to the JTextPane / JEditorPane you can add any JComponent e.g. Icon... ), example for HighlightPainter

Upvotes: 4

Related Questions