Results 1 to 8 of 8
  1. #1
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default highlighting in text area

    hello i need help on highlighting in text area after finding a word that i want to search for . i am able to search for it , but im stuck on highlighting it

    else if(action.equals("Find Text"))
    try
    {
    String search = "Enter text you wish to find";
    String out = JOptionPane.showInputDialog(search);
    if(outputArea.getText().contains(out))
    {

    Highlighter h = outputArea.getHighlighter();
    h.removeAllHighlights();
    int start= outputArea.getText().indexOf(out);
    int end = outputArea.getText().indexOf(out.length());
    outputArea.getHighlighter().addHighlight(start,end , highlightPainter);

    }
    }
    catch(BadLocationException ex)
    {
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,231
    Blog Entries
    7
    Rep Power
    24

    Default Re: highlighting in text area

    Why don't you use a simple select(start, end) call? (the method is defined in the JTextComponent parent class).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: highlighting in text area

    but how would i highlight the found text

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,231
    Blog Entries
    7
    Rep Power
    24

    Default Re: highlighting in text area

    Does your code even compile?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: highlighting in text area

    yeah , but just doesnt highlight the word i want to find

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,231
    Blog Entries
    7
    Rep Power
    24

    Default Re: highlighting in text area

    Quote Originally Posted by ajw1993 View Post
    yeah , but just doesnt highlight the word i want to find
    Really? Does this line make sense to you:

    Java Code:
    int end = outputArea.getText().indexOf(out.length());
    The indexOf( ... ) method needs an int representing a character or a String as its argument. I doubt the length of the 'out' String is what you're looking for in your text. I'd expected something like this:

    Java Code:
    int end = start+out.length();
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    ajw1993 is offline Member
    Join Date
    Nov 2012
    Posts
    30
    Rep Power
    0

    Default Re: highlighting in text area

    it works thank u , but is there any way of searching the text area without it being case sensitive

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,231
    Blog Entries
    7
    Rep Power
    24

    Default Re: highlighting in text area

    Quote Originally Posted by ajw1993 View Post
    it works thank u , but is there any way of searching the text area without it being case sensitive
    Sure, first convert the entire text to upper/lower case and do the same with the pattern you're looking for; highlight the original text accordingly.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Specifying Quoted Text by Highlighting
    By awinston in forum Suggestions & Feedback
    Replies: 0
    Last Post: 07-30-2012, 10:20 PM
  2. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  3. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  4. Need help with highlighting text in a jcombobox
    By cjmartin in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2010, 06:21 PM
  5. Replies: 1
    Last Post: 04-25-2009, 03:50 PM

Posting Permissions

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