Search code examples
javaswingjtextpanecode-editor

Java Swing. Highlight background of whole line JTextPane using Style(not Highlighter class)


How to Configure Style to make highlighting of whole line background. Like in most of IDE - current line background color is different to other code background.

Something like this:

private static final Style CURRENT_LINE = styleContext.addStyle("currentLine", defaultStyle);
static {
    StyleConstants.setBackground(CURRENT_LINE, Color.LIGHT_GRAY);
    StyleConstants.setEnd(Style.LINE_END); // This method does't exist!
}

Solution

  • Like in most of IDE - current line background color is different to other code background.

    One way to do this is to use a custom Painter. The default Painter will only highlight the area containing the text on the line. So the custom painter will need to highlight the background from the start/end of the line.

    The highlighting will need to change as the Caret moves from line to line.

    Check out Line Painter for a custom painter class that contains the above functionality.