1

I have look this post just i want know how is possible to change the foreground of a part of a text of a JTextPane with the Highlighter of java.swing and no the background.

public static void highlight(JTextPane textarea, String textToHighlight,
                                 Highlighter.HighlightPainter painter) {
        String text = textarea.getText();
        Highlighter highlighter = textarea.getHighlighter();

        if (!textToHighlight.isEmpty()) {
            //MATCH PATTERN
            Matcher m = compileWildcard(textToHighlight).matcher(text);
            while (m.find()) {
                try {
                    highlighter.addHighlight(m.start(), m.end(), painter);
                } catch (BadLocationException e) {
                    Logger.log(e);
                }
                //textarea.setCaretPosition(m.end());
            }
        }
    }

Thank in advance

  • Not with the standard look and feels. If you look at [BasicTextUI#paintSafely](https://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/basic/BasicTextUI.html#paintSafely-java.awt.Graphics-), you'll see that highlights are painted right after the background is painted. – Mihe Oct 01 '22 at 19:27
  • You set the attributes of any character of text to be a specific color. See: https://stackoverflow.com/questions/65175253/changing-text-alignment-of-specific-line-in-jtextpane/65176138#65176138 – camickr Oct 02 '22 at 01:19
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 02 '22 at 03:56

0 Answers0