Results 1 to 6 of 6
  1. #1
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default change syntax text color

    Hi again,
    This time i have a few questions.

    I want to make syntax support for my texteditor. Whenever a keyword (from the Java code) is typed, I want to change the word's color to blue.

    My code so far:

    The search funtion:
    Java Code:
    public void zoekkeywoorden(JTextComponent jTextPane1, String tezoeken) {
            try {
                Highlighter hilite = jTextPane1.getHighlighter();
                Document doc = jTextPane1.getDocument();
                String text = doc.getText(0, doc.getLength());
                int pos = 0;
    
                // Search for pattern
                while ((pos = text.indexOf(tezoeken, pos)) >= 0) {
                    // Create highlighter using private painter and apply around pattern
                    hilite.addHighlight(pos, pos+tezoeken.length(), Keywords);
                    pos += tezoeken.length();
                }
            } catch (BadLocationException e) {
            }
        }
        Highlighter.HighlightPainter Keywords = new MyHighlightPainter(Color.blue);
    The declaration of the keywords:
    Java Code:
        String [] javawords={"abstract", "do", "import", "public", "throws", "boolean",
        "double", "instanceof", "return", "transient", "break", "else", " int ", "short",
        "try", "byte", "extends", "interface", "static", "void", "case", "final", "long",
        "strictfp", "volatile", "catch", "finally", "native", "super", "while", "char", "float",
        "new", "switch", "class", "for", "package", "synchronized", "continue", "if", "private",
        "this", "default", "implements", "protected", "const", "goto", "null", "true", "false"};
    The pushbutton that will launch the search for the keywords:
    Java Code:
        private void javasyntaxActionPerformed(java.awt.event.ActionEvent evt) {                                            
            int lengte=javawords.length;
            for (int i=0;i<lengte;i++){
                zoekkeywoorden(jTextPane1,javawords[i]);
            }}
    This gives me a few problems:
    1) The search for keywords will only happen when i press the button, i would like for it to happen every time i press a key in the textpane. I tried this with a keytyped event but when i type a keyword it keeps on highlighting the text that follows. This isn't the case when i use an action event (buttonpress).

    2) I want the syntaxtext to change color instead of the highlighting but i have no idea how to write such code.

    3) When it searches for keywords, it will also find those keywords in bigger words (for exampe: the syntax keyword 'do' , the function will also highligt the do part in the word 'Undobutton'.) I want the program to only search for words. (i tried putting a space in front and behind of each string in the javawords array but then he won't be able to find keywords follow by a . or ( or... (for example he won't find the if in : if( .... ) since there is no space between the if and the ( .

    Are there simple solutions for these problems?
    Last edited by dejos456; 12-12-2009 at 09:22 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Are there simple solutions for these problems?
    Not simple ones, but possible.

    Have a look at Stephen Ostermiller's text editor. He uses

    * a tokenizer, eg, to determine when "do" should be coloured

    * a JTextPane which can display text with various attributes rather than relying on highlighting

    * a separate thread so that the colouring can go on at the same time as text is being typed. It does't have to be triggered by a button click.

    A syntax colourer is a big enough project that you will have to sit down and plan before you code. Maybe Ostermiller's code (all available) and the few words he thoughtfully added about the design may be helpful.

  3. #3
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    Thx for the reply,
    I am new to java and i find the link you gave me difficult to understand, (lots of files etc..)

    Im looking for some minor changes in my code that would allow me to come close to syntax highlighting, the text being colored instead of higlighted isn't that important.

    It's important that i can execute the search for keywords on button press (but that gives me the problem i explained above) don't know why it works on actionevent and not on a keyevent.

    The most important part is that it searches for words and not partial words.
    Isn't there some simple code to add in my code that says:
    only highlight when: non-alfabetic char+javakeyword+non-alfebetic char
    (i tried with putting space in front and behind of the keywords in string javawords, but i also need something to recognize newline chars , . ! ' etc. )

  4. #4
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    Ok i solved the searching for keywords on button press, by writing this in the keyevent:

    if(Character.isLetter(c)){
    for (int i=0;i<lengte;i++){
    zoekkeywoorden(jTextPane1,javawords[i]); }}


    Now i only need to find a way to search for words and not partial words

  5. #5
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    any ideas? cause i'm kinda stuck on that part

  6. #6
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    No solutions?
    my deadline is tommorow and i would really like the word search function to work but can't find how.

Similar Threads

  1. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 PM
  2. Color Change of data
    By Java.child in forum AWT / Swing
    Replies: 20
    Last Post: 02-12-2009, 06:51 AM
  3. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 AM
  4. Replies: 0
    Last Post: 06-25-2008, 10:33 AM
  5. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •