Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Syntax Highlighting in Editor

  1. #1
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Syntax Highlighting in Editor

    Here is the original code I found on StackOverFlow. It highlights some specific words in a JTextPane.

    import javax.swing.*;
    import java.awt.*;
    import javax.swing.text.*;
     
    public class ColorChangeWhileTyping extends JFrame{
     
     
     
        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 ColorChangeWhileTyping () {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 400);
            setLocationRelativeTo(null);
     
            final StyleContext cont = StyleContext.getDefaultStyleContext();
            final AttributeSet attr = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
            final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
            DefaultStyledDocument doc = new DefaultStyledDocument() {
                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)*(private|public|protected)"))
                                setCharacterAttributes(wordL, wordR - wordL, attr, false);
                            else
                                setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                            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)*(private|public|protected)")) {
                        setCharacterAttributes(before, after - before, attr, false);
                    } else {
                        setCharacterAttributes(before, after - before, attrBlack, false);
                    }
                }
            };
            JTextPane txt = new JTextPane(doc);
            txt.setText("public class Hi {}");
            add(new JScrollPane(txt));
            setVisible(true);
        }
     
        public static void main (String args[]) {
            new ColorChangeWhileTyping();
        }
     
    }


    I Modified it a little to suit my purpose as the orginal code was highlighting text with only one colour.
    I want to highlight different words with different colour.
    Here is the modified code

     
    import java.awt.Color;
    import java.util.List;
    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 KeyWordCheck extends JFrame {
     
        List section;
        JTextPane textArea;
     
        String red = "section";
        String blue = ".bss|.data|.text";
        String mainString = "section|.bss|.data|.text";
     
        private int findLastNonWordChar(String text, int index) {
            while (--index >= 0) {
                if (String.valueOf(text.charAt(index)).matches(mainString)) {
                    break;
                }
            }
            return index;
        }
     
        private int findFirstNonWordChar(String text, int index) {
            while (index < text.length()) {
                if (String.valueOf(text.charAt(index)).matches(mainString)) {
                    break;
                }
                index++;
            }
            return index;
        }
     
        JTextPane KeyWorPane;
     
        public KeyWordCheck() {
     
             setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 400);
            setLocationRelativeTo(null);
     
            final StyleContext cont = StyleContext.getDefaultStyleContext();
     
            final AttributeSet attrRed = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.RED);
     
            final AttributeSet attrBlack = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLACK);
     
            final AttributeSet attrBlue = cont.addAttribute(cont.getEmptySet(), StyleConstants.Foreground, Color.BLUE);
     
            DefaultStyledDocument doc = new DefaultStyledDocument() {
                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(mainString)) {
                            if (text.substring(wordL, wordR).matches(red)) {
                                setCharacterAttributes(wordL, wordR - wordL, attrRed, false);
                            }
                            else if (text.substring(wordL, wordR).matches(blue)) {
                                setCharacterAttributes(wordL, wordR - wordL, attrBlue, false);
                            }
                            else {
                                setCharacterAttributes(wordL, wordR - wordL, attrBlack, false);
                            }
                            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(red)) {
                        setCharacterAttributes(before, after - before, attrRed, false);
                    }
                     else if (text.substring(before, after).matches(blue)) {
                        setCharacterAttributes(before, after - before, attrBlue, false);
                    } else {
                        setCharacterAttributes(before, after - before, attrBlack, false);
                    }
                }
     
            }; 
            textArea = new JTextPane(doc);
     
     
            add(new JScrollPane(textArea));
            setVisible(true);
        }
    public static void main(String args[])
    {
        new KeyWordCheck();
    }
    }

    I dont know for some reason it doesnt work correctly. The colour goes away after I press Space to write the next word
    Please Help!!!
    ăϻі


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,780
    My Mood
    Mellow
    Thanks
    175
    Thanked 625 Times in 612 Posts

    Default Re: Syntax Highlighting in Editor

    The colour goes away after I press Space to write the next word
    Find the action triggered when the next character or space is entered and determine why it (the action) is causing the color to change.

  3. #3
    Junior Member amitection's Avatar
    Join Date
    May 2014
    Location
    India
    Posts
    24
    My Mood
    Busy
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Re: Syntax Highlighting in Editor

    Well I figured it out. The Main Reason it was not working was that i have removed the "\\W" used for comparison by Word
    ăϻі

Similar Threads

  1. Syntax highlighting
    By newbie in forum Forum Updates & Feedback
    Replies: 1
    Last Post: October 4th, 2013, 12:46 PM
  2. Need help in highlighting tree nodes
    By rahulha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 18th, 2013, 08:14 AM
  3. Syntax Highlighting errors and weirdness?
    By sci4me in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 27th, 2013, 10:25 PM
  4. java syntax highlighting
    By Saulius in forum Java Theory & Questions
    Replies: 2
    Last Post: August 24th, 2011, 05:47 AM
  5. New Syntax Highlighting Feature!
    By JavaPF in forum Forum Updates & Feedback
    Replies: 15
    Last Post: July 14th, 2010, 09:20 AM

Tags for this Thread