I am trying to search and highlight words in my JTextPane. It works wonderfully with words that do not have many results so far, but when I try to search for a word in which there are so many examples, sometimes Highlighter shows the path to the result, As it remembers with many letters Anyway, here's the code that I used at the end.
int index = 0; String text = null; {Int length = textPane.getDocument (). Try it. GetLength (); Text = textPane.getDocument (). GetText (0, Length); } Catch (Bad leonation expansion E1) {// To-O auto-generated catch block e1.printStackTrace (); } Try {while (index = text.indexOf (myWord, index)) gt; = 0) {DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter (Color.YELLOW); TextPane.getHighlighter () AddHighlight (index, index + myWord.length (), highlight pane); Index + = myWord.length (); }} Grip (Badlocon exception e) {// Todo auto-generated catch block e.printStackTrace (); }} Here is a screenshot that describes the problem red circle = false result, green circle = right result
Thank you in advance for your help.
I do not know whether Highlighter is supported to use or not. What I can tell, Highlighter is only used by javax.swing.text.View and its related sections.
I will do it this way:
StyledDocument Document = textPane.getStyledDocument (); Style Highlight = document.addStyle ("Highlight", tap); StyleConstants.setBackground (Highlight, Color. YELLOW); String text = document.getText (0, document.getLength ()); While ((index = text.indexoff (myword, index))> gt; = 0} {document.setCharacterAttributes (index, myWord.length (), highlight, false); Index + = myWord.length (); }
No comments:
Post a Comment