Results 1 to 4 of 4
- 04-02-2012, 09:21 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
[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: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":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
How do I translate string index to JTextPane coordinate?
Sincerely,
Marko
- 04-03-2012, 06:27 AM #2
Re: [JTextPane]Question regarding highligting text
Go through camickr's blog entry on Text and New Lines.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-03-2012, 07:24 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Re: [JTextPane]Question regarding highligting text
THANKS MAN IT WORKS NOW!!!!! I am very grateful for helping me!!!!!
- 04-03-2012, 10:11 AM #4
Similar Threads
-
How to email formatted text from a JTextPane
By akssingh400 in forum AWT / SwingReplies: 2Last Post: 02-23-2011, 07:59 PM -
[QUESTION]Get text from image?
By ThomasH in forum Java 2DReplies: 2Last Post: 06-06-2010, 06:37 PM -
Add text to JTextPane with color
By PhQ in forum New To JavaReplies: 4Last Post: 04-01-2010, 07:26 PM -
How to Get Text(RichText) from JTextPane
By dorairaj in forum AWT / SwingReplies: 3Last Post: 07-15-2009, 07:33 AM -
Insert text at abitrary point in a JTextPane
By angues in forum AWT / SwingReplies: 6Last Post: 02-02-2009, 04:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks