Java JTextPane applyRegex(String regex, JTextPane pane, Color highlightColor)

Here you can find the source of applyRegex(String regex, JTextPane pane, Color highlightColor)

Description

apply Regex

License

Open Source License

Declaration

public static void applyRegex(String regex, JTextPane pane, Color highlightColor) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void applyRegex(String regex, JTextPane pane, Color highlightColor) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(pane.getText());
        while (matcher.find()) {
            StyleContext sc = StyleContext.getDefaultStyleContext();
            AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground,
                    highlightColor);// w ww  .  j  a  v a 2  s. c o  m
            pane.getStyledDocument().setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), aset,
                    true);
        }
    }
}

Related

  1. addStyle(JTextPane textPane, String name, Color foreground)
  2. appendAsGreen(JTextPane pane, String fontName, int fontSize, String s)
  3. appendAsMessage(JTextPane pane, String fontName, int fontSize, String s)
  4. appendString(String str, JTextPane jtext)
  5. appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor)
  6. centerText(JTextPane pane)
  7. createJTextPane(String text, Color backgroundColor)
  8. createTextPaneScrollPane(JTextPane textPane)
  9. getFontFamily(JTextPane textPane)