Results 1 to 4 of 4
  1. #1
    KernelPanic is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Exclamation [JTextPane]Question regarding highligting text

    Dear Sirs and Madams!

    I am working on simple text editor, which functionality also includes Highlighting searched text. Here is situation: User loads text file into JTextPane. Then it selects Text Search tool and enters text to be searched. It confirms text search function with pressing "Mark All" button, which highlights all searched hits in text. But here is the problem - here is my code:
    Java Code:
        @SuppressWarnings("unchecked")
        private void stringOperationMarkAll(final String strSearch)
        {
            int iIndex=0;
            int iNumberOccurences=0;
            String strFileContent=this.textEditor().textArea().getText().toString();
            String strRegExpInput="\\b"+strSearch+"\\b";    // constructs regular expression from searched string
            Pattern p=Pattern.compile(strRegExpInput);
            Matcher m=p.matcher(strFileContent);
            CTextIndex cti=null;
            ArrayList listTextIndexes=new ArrayList();
    
            while(m.find())
            {
                iNumberOccurences++;
                listTextIndexes.add(new CTextIndex(m.start(),
                                                   m.end()));
            }   // while
            try
            {
                this.colorMatches(listTextIndexes,
                                  Color.GREEN);
            }
            catch(BadLocationException ex)
            {
                Logger.getLogger(CFrameMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }   // stringOperationMarkAll
    
        private void stringOperationReplaceAll(final String strSearch,
                                               final String strReplaceWith)
        {
        }   // stringOperationReplaceAll
    
        private void colorMatches(final ArrayList<CTextIndex> listMatches,
                                  final Color c) throws BadLocationException
        {
            for(int iIndex=0; iIndex<listMatches.size(); iIndex++)
            {
                this.textEditor().textArea().getHighlighter().addHighlight(listMatches.get(iIndex).indexBegin(),
                                                                           (listMatches.get(iIndex).indexEnd()-1),
                                                                           new DefaultHighlighter.DefaultHighlightPainter(c));
            }   // for
        }   // color matches
    In the first method, I gather start and end indexes of matched words in string, represented in JTextPane. The second method colorMathces highlights words, that are available in list of hits (parameter final ArrayList<CTextIndex>). The highlightaion shows, but in the wrong words. Why? I attached screenshot for windows file hosts and search parameter "127":[JTextPane]Question regarding highligting text-errorhighlight.pngHow do I translate string index to JTextPane coordinate?

    Sincerely,
    Marko

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: [JTextPane]Question regarding highligting text

    Go through camickr's blog entry on Text and New Lines.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    KernelPanic is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: [JTextPane]Question regarding highligting text

    THANKS MAN IT WORKS NOW!!!!! I am very grateful for helping me!!!!!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: [JTextPane]Question regarding highligting text

    Don't forget to thank camickr whenever you see him around.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to email formatted text from a JTextPane
    By akssingh400 in forum AWT / Swing
    Replies: 2
    Last Post: 02-23-2011, 07:59 PM
  2. [QUESTION]Get text from image?
    By ThomasH in forum Java 2D
    Replies: 2
    Last Post: 06-06-2010, 06:37 PM
  3. Add text to JTextPane with color
    By PhQ in forum New To Java
    Replies: 4
    Last Post: 04-01-2010, 07:26 PM
  4. How to Get Text(RichText) from JTextPane
    By dorairaj in forum AWT / Swing
    Replies: 3
    Last Post: 07-15-2009, 07:33 AM
  5. Insert text at abitrary point in a JTextPane
    By angues in forum AWT / Swing
    Replies: 6
    Last Post: 02-02-2009, 04:08 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
  •