0

I have a problem with some code I have written to go through the text in a JTextPane and highlight key words ("hello")

it works fine, unless I delete a character off of one of the highlighted words, then it doesn't update the styles anymore... here is my code:

public class ScriptEditor extends JFrame
{
    private static final long serialVersionUID = 1L;

    private static int height = 340;
    private static int width = 675;

    private boolean saved = false;

    public Pattern matcher = Pattern.compile("hello");

    private JTextPane editor = new JTextPane();

    public ScriptEditor()
    {
        super("Script Editor");
        setJMenuBar(new ScriptMenu());
        setSize(width, height);

        editor.getDocument().addDocumentListener(new DocumentListener() 
        {
            public void changedUpdate(DocumentEvent event) {}

            public void insertUpdate(DocumentEvent event)
            {
                checkForHighlights();
            }

            public void removeUpdate(DocumentEvent event)
            {
                checkForHighlights();
            }
        });

        add(new JScrollPane(editor));
    }

    private void checkForHighlights()
    {
        Runnable checker = new Runnable() 
        {
            public void run()
            {
                Matcher stringMatcher;
                try
                {
                    stringMatcher = matcher.matcher(editor.getDocument().getText(0, editor.getDocument().getLength()));

                    StyleContext style = StyleContext.getDefaultStyleContext();
                    AttributeSet black = style.addAttribute(style.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
                    AttributeSet red = style.addAttribute(style.getEmptySet(), StyleConstants.Foreground, Color.RED);

                    editor.getStyledDocument().setCharacterAttributes(editor.getDocument().getLength(),     editor.getDocument().getLength(), style.getEmptySet(), true);

                    while (stringMatcher.find())
                    {
                        editor.getStyledDocument().setCharacterAttributes(stringMatcher.start(), stringMatcher.end() - stringMatcher.start(), red, false);
                    }
                }
                catch (BadLocationException e)
                {
                    e.printStackTrace();
                }
            }
        };
        SwingUtilities.invokeLater(checker);
    }

this is the sequence of events in testing:

type: "hello" (result is red highlight) type: " world" (result is red highlight round hello and black for " world" type: "hello" (result is read highlight, as expected) type: space and then delete, or just delete (no change) type: test (result is test is highlighted red!!!)

picture: enter image description here

can anybody tell me what I am doing wrong here? How can I fix this!?

Thanks

J_mie6
  • 720
  • 1
  • 9
  • 25
  • Use a `DocumentFilter`, not a `DocumentListener`, the contents have already being committed to the document by the time the `DocumentListener` is applied – MadProgrammer Feb 14 '14 at 22:50
  • As a possible [example](http://stackoverflow.com/questions/19399904/set-hilighting-color-in-jtextfield/19401765#19401765), while it uses a `JTextField`, the concept should translate. You could also take a look at [java-syntax-highlighter](https://code.google.com/p/java-syntax-highlighter/) and [Syntax Highlighter for Java](http://stackoverflow.com/questions/1853419/syntax-highlighter-for-java) – MadProgrammer Feb 14 '14 at 22:59
  • it is more change in text colour than highlighting... I added a document filter (and ran the chekcForHighlights() method in the insert string, replace and remove methods, but it gave no change on the above code...), but I'm not sure if I implemented it right – J_mie6 Feb 14 '14 at 23:21
  • Also, why is the line just before the while loop clearing the entire document's highlighting as I expect it to? I thought that when I used that method it would make everything black again then the matcher would find hellos in the string – J_mie6 Feb 14 '14 at 23:24
  • ok I fixed it, I instead of a 0 in the reset line I had a duplicate doc.getLength(), whoops! would still appreciate improvements – J_mie6 Feb 14 '14 at 23:35
  • Text coloring, highlighting, it's pretty much the same thing in this context ;) – MadProgrammer Feb 15 '14 at 02:03

0 Answers0