java - efficient way to color text in JTextPane -


i have problem regarding coloring keywords in jtextpane. in other words, want make mini ide write code , want give color (say blue) keywords "public" "private" ... etc. problem is slow !! each time hit "space" or "backspace" key function scans whole text give color keywords, when write lot of code in textpane gets slow. here function of matching keywords:

public void matchword() throws badlocationexception {         string tokens[] = arabicparser.tokennames;         int index = 0;         string textstr[] = textpane.gettext().split("\\r?\\n");         for(int i=0 ; i<textstr.length ; i++) {             string t = textstr[i];             stringtokenizer ts2 = new stringtokenizer(t, " ");             while(ts2.hasmoretokens()) {                 string token = ts2.nexttoken();                  // iterations reduced removing 16 symbols search space                 for(int j = 3 ; j<tokens.length-5 ; j++) {                     if(!(token.equals("؛")) && (tokens[j].equals("'"+token+"'"))) {                         changecolor(textpane,token,color.blue,index,token.length());                         break;                     } else {                         changecolor(textpane,token,color.black,index,token.length());                     }                 }                 index += token.length() + 1;             }             //index -= 1;         }     } 

and here function of coloring matched words:

private void changecolor(jtextpane tp, string msg, color c, int beginindex, int length) throws badlocationexception {         simpleattributeset sas = new simpleattributeset();          styleconstants.setforeground(sas, c);         styleddocument doc = (styleddocument)tp.getdocument();         doc.setcharacterattributes(beginindex, length, sas, false);         sas = new simpleattributeset();          styleconstants.setforeground(sas, color.black);         tp.setcharacterattributes(sas, false);     } 

and in advance =)

consider replacing stringtokenizer since it's modern use discoraged https://stackoverflow.com/a/6983908/1493294

consider refactoring string tokens[] hashset<string> tokens. hash lookup faster looping, tokens[] gets large.

if you'd use more 2 colors try hashmap<string, color> tokens.

also, having 2 different things called token , tokens running around in here confusing. consider renaming tokens[] colorednames[] it's different token textpane tokens.

consider using profiler see bulk of time being spent. might find repetitive work being done in changecolor() worth caching.

if write class called colorchanger. colorchanger have 1 constructor , 1 method changecolor(). constructor take (and cache) parameters don't change loop. colorchanger.changecolor() take parameters change loop.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -