2

So I got this code from this link on SO and I got it to do what I wanted, but after I backspace a word of one color, all preceding words are that color, too.

result without glitch activated

Result: These words are yellow: yel, yw. This word is red: rd. These words are blue: blu, be.

result with glitch activated

Result: These words are yellow: yel, yw. This word is red: rd. These words are blue: blu, be.

I backspaced one of the yellow words. Now all of my words are yellow.

Code:

package test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ChangeFontColor extends JFrame{
private static final long serialVersionUID = 2121337395232340746L;
public ChangeFontColor() {
    super("Change Font Color");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(1100, 700);
    setLocationRelativeTo(null);

    final StyleContext cont = StyleContext.getDefaultStyleContext();
    final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
    final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW);
    final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);

    DefaultStyledDocument doc = new DefaultStyledDocument() {
        private static final long serialVersionUID = -3442280878563016288L;

        public void insertString (int offset, String str, AttributeSet a) throws BadLocationException {
            super.insertString(offset, str, a);

            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offset);
            if (before < 0) 
                before = 0;
            int after = findFirstNonWordChar(text, offset + str.length());
            int wordL = before;
            int wordR = before;

            while(wordR <= after) {
                if(wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
                    if(text.substring(wordL, wordR).matches("(\\W)*(yellow|yel|yw)")){
                        setCharacterAttributes(wordL, wordR - wordL, attrYel, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(red|rd)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrRed, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(blue|blu|be)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrBlu, true);
                    }
                    wordL = wordR;
                }
                wordR++;
            }
        }

        public void remove (int offs, int len) throws BadLocationException {
            super.remove(offs, len);

            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offs);
            if (before < 0) 
                before = 0;
            int after = findFirstNonWordChar(text, offs);

            if(text.substring(before, after).matches("(\\W)*(yellow|yel|yw)")) {
                setCharacterAttributes(before, after - before, attrYel, true);

            }else if((text.substring(before, after).matches("(\\W)*(red|rd)"))){
                setCharacterAttributes(before, after - before, attrRed, true);
            }else if((text.substring(before, after).matches("(\\W)*(blue|blu|be)"))){
                setCharacterAttributes(before, after - before, attrBlu, true);
            }
        }
    };
    JTextPane txt = new JTextPane(doc);
    txt.setBackground(Color.DARK_GRAY);
    txt.setForeground(Color.WHITE);
    txt.setCaretColor(Color.WHITE);
    txt.setFont(new Font("Serif", Font.PLAIN, 18));
    add(new JScrollPane(txt));
    setVisible(true);
}

private int findLastNonWordChar (String text, int index) {
    while (--index >= 0) {
        if (String.valueOf(text.charAt(index)).matches("\\W")) {
            break;
        }
    }
    return index;
}

private int findFirstNonWordChar (String text, int index) {
    while (index < text.length()) {
        if (String.valueOf(text.charAt(index)).matches("\\W")) {
            break;
        }
        index++;
    }
    return index;
}

public static void main (String args[]) {
    new ChangeFontColor();
}
}

I found a solution:

package test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ChangeFontColor extends JFrame{
private static final long serialVersionUID = 2121337395232340746L;
public ChangeFontColor() {
    super("Change Font Color");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(1100, 700);
    setLocationRelativeTo(null);

    final StyleContext cont = StyleContext.getDefaultStyleContext();
    final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
    final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW);
    final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
    final AttributeSet attrWhite = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.WHITE);

    DefaultStyledDocument doc = new DefaultStyledDocument() {
        private static final long serialVersionUID = -3442280878563016288L;

        public void insertString (int offset, String str, AttributeSet a) throws BadLocationException {
            super.insertString(offset, str, a);

            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offset);
            if (before < 0) 
                before = 0;
            int after = findFirstNonWordChar(text, offset + str.length());
            int wordL = before;
            int wordR = before;

            while(wordR <= after) {
                if(wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
                    if(text.substring(wordL, wordR).matches("(\\W)*(yellow|yel|yw)")){
                        setCharacterAttributes(wordL, wordR - wordL, attrYel, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(red|rd)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrRed, true);

                    }else if((text.substring(wordL, wordR).matches("(\\W)*(blue|blu|be)"))){
                        setCharacterAttributes(wordL, wordR - wordL, attrBlu, true);
                    }else{
                        setCharacterAttributes(wordL, wordR - wordL, attrWhite, true);
                    }
                    wordL = wordR;
                }
                wordR++;
            }
        }

        public void remove (int offs, int len) throws BadLocationException {
            super.remove(offs, len);

            String text = getText(0, getLength());
            int before = findLastNonWordChar(text, offs);
            if (before < 0) 
                before = 0;
            int after = findFirstNonWordChar(text, offs);

            if(text.substring(before, after).matches("(\\W)*(yellow|yel|yw)")) {
                setCharacterAttributes(before, after - before, attrWhite, true);

            }else if((text.substring(before, after).matches("(\\W)*(red|rd)"))){
                setCharacterAttributes(before, after - before, attrRed, true);
            }else if((text.substring(before, after).matches("(\\W)*(blue|blu|be)"))){
                setCharacterAttributes(before, after - before, attrBlu, true);
            }else{
                setCharacterAttributes(before, after - before, attrWhite, true);
            }
        }
    };
    JTextPane txt = new JTextPane(doc);
    txt.setBackground(Color.DARK_GRAY);
    txt.setForeground(Color.WHITE);
    txt.setCaretColor(Color.WHITE);
    txt.setFont(new Font("Serif", Font.PLAIN, 18));
    add(new JScrollPane(txt));
    setVisible(true);
}

private int findLastNonWordChar (String text, int index) {
    while (--index >= 0) {
        if (String.valueOf(text.charAt(index)).matches("\\W")) {
            break;
        }
    }
    return index;
}

private int findFirstNonWordChar (String text, int index) {
    while (index < text.length()) {
        if (String.valueOf(text.charAt(index)).matches("\\W")) {
            break;
        }
        index++;
    }
    return index;
}

public static void main (String args[]) {
    new ChangeFontColor();
}
}
Community
  • 1
  • 1
  • 1
    Is the last sentence colored by your algorithm or is it the inputAttributes that are wrong? Use a debugger in any ide to check. – Sharcoux Sep 01 '16 at 08:08

1 Answers1

0

I found a solution:

package test;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class ChangeFontColor extends JFrame{
private static final long serialVersionUID = 2121337395232340746L;
public ChangeFontColor() {
super("Change Font Color");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1100, 700);
setLocationRelativeTo(null);

final StyleContext cont = StyleContext.getDefaultStyleContext();
final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
final AttributeSet attrYel = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.YELLOW);
final AttributeSet attrBlu = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
final AttributeSet attrWhite = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.WHITE);

DefaultStyledDocument doc = new DefaultStyledDocument() {
    private static final long serialVersionUID = -3442280878563016288L;

    public void insertString (int offset, String str, AttributeSet a) throws BadLocationException {
        super.insertString(offset, str, a);

        String text = getText(0, getLength());
        int before = findLastNonWordChar(text, offset);
        if (before < 0) 
            before = 0;
        int after = findFirstNonWordChar(text, offset + str.length());
        int wordL = before;
        int wordR = before;

        while(wordR <= after) {
            if(wordR == after || String.valueOf(text.charAt(wordR)).matches("\\W")) {
                if(text.substring(wordL, wordR).matches("(\\W)*(yellow|yel|yw)")){
                    setCharacterAttributes(wordL, wordR - wordL, attrYel, true);

                }else if((text.substring(wordL, wordR).matches("(\\W)*(red|rd)"))){
                    setCharacterAttributes(wordL, wordR - wordL, attrRed, true);

                }else if((text.substring(wordL, wordR).matches("(\\W)*(blue|blu|be)"))){
                    setCharacterAttributes(wordL, wordR - wordL, attrBlu, true);
                }else{
                    setCharacterAttributes(wordL, wordR - wordL, attrWhite, true);
                }
                wordL = wordR;
            }
            wordR++;
        }
    }

    public void remove (int offs, int len) throws BadLocationException {
        super.remove(offs, len);

        String text = getText(0, getLength());
        int before = findLastNonWordChar(text, offs);
        if (before < 0) 
            before = 0;
        int after = findFirstNonWordChar(text, offs);

        if(text.substring(before, after).matches("(\\W)*(yellow|yel|yw)")) {
            setCharacterAttributes(before, after - before, attrWhite, true);

        }else if((text.substring(before, after).matches("(\\W)*(red|rd)"))){
            setCharacterAttributes(before, after - before, attrRed, true);
        }else if((text.substring(before, after).matches("(\\W)*(blue|blu|be)"))){
            setCharacterAttributes(before, after - before, attrBlu, true);
        }else{
            setCharacterAttributes(before, after - before, attrWhite, true);
        }
    }
};
JTextPane txt = new JTextPane(doc);
txt.setBackground(Color.DARK_GRAY);
txt.setForeground(Color.WHITE);
txt.setCaretColor(Color.WHITE);
txt.setFont(new Font("Serif", Font.PLAIN, 18));
add(new JScrollPane(txt));
setVisible(true);
}

private int findLastNonWordChar (String text, int index) {
while (--index >= 0) {
    if (String.valueOf(text.charAt(index)).matches("\\W")) {
        break;
    }
}
return index;
}

private int findFirstNonWordChar (String text, int index) {
while (index < text.length()) {
    if (String.valueOf(text.charAt(index)).matches("\\W")) {
        break;
    }
    index++;
}
return index;
}

public static void main (String args[]) {
new ChangeFontColor();
}
}