Results 1 to 6 of 6
Thread: change syntax text color
- 12-12-2009, 09:20 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
change syntax text color
Hi again,
This time i have a few questions.
I want to make syntax support for my texteditor. Whenever a keyword (from the Java code) is typed, I want to change the word's color to blue.
My code so far:
The search funtion:
The declaration of the keywords:Java Code:public void zoekkeywoorden(JTextComponent jTextPane1, String tezoeken) { try { Highlighter hilite = jTextPane1.getHighlighter(); Document doc = jTextPane1.getDocument(); String text = doc.getText(0, doc.getLength()); int pos = 0; // Search for pattern while ((pos = text.indexOf(tezoeken, pos)) >= 0) { // Create highlighter using private painter and apply around pattern hilite.addHighlight(pos, pos+tezoeken.length(), Keywords); pos += tezoeken.length(); } } catch (BadLocationException e) { } } Highlighter.HighlightPainter Keywords = new MyHighlightPainter(Color.blue);
The pushbutton that will launch the search for the keywords:Java Code:String [] javawords={"abstract", "do", "import", "public", "throws", "boolean", "double", "instanceof", "return", "transient", "break", "else", " int ", "short", "try", "byte", "extends", "interface", "static", "void", "case", "final", "long", "strictfp", "volatile", "catch", "finally", "native", "super", "while", "char", "float", "new", "switch", "class", "for", "package", "synchronized", "continue", "if", "private", "this", "default", "implements", "protected", "const", "goto", "null", "true", "false"};
This gives me a few problems:Java Code:private void javasyntaxActionPerformed(java.awt.event.ActionEvent evt) { int lengte=javawords.length; for (int i=0;i<lengte;i++){ zoekkeywoorden(jTextPane1,javawords[i]); }}
1) The search for keywords will only happen when i press the button, i would like for it to happen every time i press a key in the textpane. I tried this with a keytyped event but when i type a keyword it keeps on highlighting the text that follows. This isn't the case when i use an action event (buttonpress).
2) I want the syntaxtext to change color instead of the highlighting but i have no idea how to write such code.
3) When it searches for keywords, it will also find those keywords in bigger words (for exampe: the syntax keyword 'do' , the function will also highligt the do part in the word 'Undobutton'.) I want the program to only search for words. (i tried putting a space in front and behind of each string in the javawords array but then he won't be able to find keywords follow by a . or ( or... (for example he won't find the if in : if( .... ) since there is no space between the if and the ( .
Are there simple solutions for these problems?Last edited by dejos456; 12-12-2009 at 09:22 PM.
- 12-12-2009, 10:18 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Not simple ones, but possible.Are there simple solutions for these problems?
Have a look at Stephen Ostermiller's text editor. He uses
* a tokenizer, eg, to determine when "do" should be coloured
* a JTextPane which can display text with various attributes rather than relying on highlighting
* a separate thread so that the colouring can go on at the same time as text is being typed. It does't have to be triggered by a button click.
A syntax colourer is a big enough project that you will have to sit down and plan before you code. Maybe Ostermiller's code (all available) and the few words he thoughtfully added about the design may be helpful.
- 12-13-2009, 10:06 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Thx for the reply,
I am new to java and i find the link you gave me difficult to understand, (lots of files etc..)
Im looking for some minor changes in my code that would allow me to come close to syntax highlighting, the text being colored instead of higlighted isn't that important.
It's important that i can execute the search for keywords on button press (but that gives me the problem i explained above) don't know why it works on actionevent and not on a keyevent.
The most important part is that it searches for words and not partial words.
Isn't there some simple code to add in my code that says:
only highlight when: non-alfabetic char+javakeyword+non-alfebetic char
(i tried with putting space in front and behind of the keywords in string javawords, but i also need something to recognize newline chars , . ! ' etc. )
- 12-13-2009, 10:39 AM #4
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Ok i solved the searching for keywords on button press, by writing this in the keyevent:
if(Character.isLetter(c)){
for (int i=0;i<lengte;i++){
zoekkeywoorden(jTextPane1,javawords[i]); }}
Now i only need to find a way to search for words and not partial words
- 12-13-2009, 06:27 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
any ideas? cause i'm kinda stuck on that part
- 12-14-2009, 11:09 AM #6
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Change the color in my program
By carl in forum New To JavaReplies: 5Last Post: 04-03-2009, 12:20 PM -
Color Change of data
By Java.child in forum AWT / SwingReplies: 20Last Post: 02-12-2009, 06:51 AM -
How to change string Color
By Java.child in forum AWT / SwingReplies: 3Last Post: 01-06-2009, 04:27 AM -
How to use TextAttribute class to change color and font of a text
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:33 AM -
How to change TXT color Onclick
By dave700800 in forum New To JavaReplies: 1Last Post: 12-08-2007, 01:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks