java - Letters in JTextPane are colored after delay -
i have simple text editor colours java key words blue. code:
class mainpanel extends jpanel { private int width = 800; private int height = 500; private jframe frame; private jtextpane codepane = new jtextpane(); private styleddocument doc = codepane.getstyleddocument(); mainpanel(jframe frame) { this.frame = frame; setpreferredsize(new dimension(width, height)); setlayout(new borderlayout()); jscrollpane scroll = new jscrollpane(codepane); add(scroll, borderlayout.center); codepane.addkeylistener(new mainpanel.keyhandler()); codepane.setfont(new font("monospaced", font.plain, 15)); //loading key words.. //... } private class keyhandler extends keyadapter { @override public void keytyped(keyevent ev) { string code = codepane.gettext(); simpleattributeset set = new simpleattributeset(); styleconstants.setforeground(set, color.black); doc.setcharacterattributes(0, code.length(), set, true); //change keywords color int lastindex = 0; (int = 0; < words.length; a++) { set = new simpleattributeset(); if (arrays.aslist(keywords).contains(words[a])) { styleconstants.setforeground(set, color.blue); } doc.setcharacterattributes(lastindex, words[a].length(), set, true); lastindex += words[a].length() + 1; //+1 bo jeszcze spacja po słowie } } } }
my problem text highlighting (in keytyped event) takes place before placing letter in text area. when type in: "int" wont colour blue when type 1 more character "int" coloured blue eg. "intr", "int" coloured blue , r letter black. how prevent it? 1 solution replace keytyped keyreleased cant because i’m planning things while enter , tab pressed , need use consume method on them doesn’t work on keyreleased.
one solution replace keytyped keyreleased
don't use keylistener. should using documentlistener. see section swing tutorial on how write document listener more information.
im planning things while enter , tab
you should using key bindings. @ table of contents above link , find section on how use key bindings
.
Comments
Post a Comment