CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Highlighting a word in a JTextArea

    Hello, I am in need of a solution to solve a problem I am having right now.
    The problem is to highlight a word in a JTextArea.
    I am able to highlight a single character in the JTextArea by using the code below:

    Code:
    if (detectionMethod2 == true) {
                    String chars = "{";
                    comp = jTextArea1;
                    charsToHighlight = chars;
    
                    Highlighter h = comp.getHighlighter();
                    h.removeAllHighlights();
                    String text = comp.getText();
    
                    for (int j=0;j<text.length();j++) {
                        char ch = text.charAt(j);
                        if (charsToHighlight.indexOf(ch) >= 0) {
                            try {
                                h.addHighlight(j, j+1, DefaultHighlighter.DefaultPainter);
                            } catch (Exception ble) {
                            }
                        }
                    }
                }
    However, I have no idea how to convert or change the above to ONLY highlight a word that I want. For example, "int" without the quotation marks. It will only highlight the word "int" and nothing else, not even integer or anything that has "int" in it.

    Hopefully someone will be able to help me out there. Thank you.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Highlighting a word in a JTextArea

    If you want to highlight a word then find the index of the word you want to highlight and the length of the word and use these two values to set up the highlight eg

    Code:
    int index = text.indexOf(myWord);
    int len = myWord.length();
    highlighter.addHighlight(index, index+len, DefaultHighlighter.DefaultPainter);
    If you want to highlight every instance of a word then wrap the above in a loop, repeating until there are no more occurrences of the word eg

    Code:
    int index = text.indexOf(myWord);
    
    while ( index >= 0 ) {
        int len = myWord.length();
        highlighter.addHighlight(index, index+len, DefaultHighlighter.DefaultPainter);
        index = text.indexOf(myWord, index+len);
    }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Highlighting a word in a JTextArea

    Thanks, keang!

    It works like a charm! Cheers!

  4. #4
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Highlighting a word in a JTextArea

    Sorry for the double posts...

    Just another question... How do I make it to only highlight a particular word?

    For example, I want it to highlight "ump" which means that it will only highlight "ump" and not "jump", "lump", "umpire", and etc.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Highlighting a word in a JTextArea

    That's not quite so easy. You could do this by using a regex to find the index of complete words eg "\b[Uu]mp\b" would find all instances of ump or Ump where they are complete words.

    Edit: If you're not sure how to do this using a regex check out the demo at http://www.keang.co.uk/regex.html. The code is given at the bottom of the page.
    Last edited by keang; August 23rd, 2011 at 03:51 AM. Reason: URL added
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Highlighting a word in a JTextArea

    that seems to be what I want to do, but it's not that much stuff.

    I tried with this:

    Code:
    Scanner sc = new Scanner("Something int some integer some stuff and int and some more int with integer and intabc abcint int int");
    
    if(sc.hasNext("\bint\b"))
    System.out.println("hit");
    but I guess it doesn't work that way? because it is not printing hit...
    hahah...
    I am trying to modify your codes to make it more simpler and to only detect one thing, but I don't seem to be able to... hmmm...

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Highlighting a word in a JTextArea

    I tried with this:
    You can't use Scanner because it won't give you the index of the word it finds and you can't just use a regex string pattern where a string is expected. Finally you need to escape the backslash characters when using a backslash in a string in Java.

    I am trying to modify your codes to make it more simpler and to only detect one thing,
    I thought you wanted every occurrence of a word and not just one occurrence.

    Assuming you do want every occurrence the bit of code you need is something like:
    Code:
    Pattern pattern;
        try
            {
            pattern = Pattern.compile("\\b"+myWord+"\\b");
            }
        catch(Exception e)
            {
            e.printStacktrace();
            return;
            }
    
        Matcher matcher = pattern.matcher(inputStr);
    
        while( matcher.find() )
            {
            int start = matcher.start();
            int end = matcher.end();
    
            highlighter.addHighlight(start, end, DefaultHighlighter.DefaultPainter);
            }
    I've just written this here without even compiling or testing it so it may contain mistakes, but the gist of it is what you need.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Highlighting a word in a JTextArea

    oh yea! that's exactly what I needed!

    thanks again!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured