Giannis Giannis - 2 years ago 64
Java Question

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).

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download