aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes wriring code for an EDITOR  big problem?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "wriring code for an EDITOR  big problem??" Watch "wriring code for an EDITOR  big problem??" New topic
Author

wriring code for an EDITOR big problem??

vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
Hi, i am developing code for an editor like tool. as we work in some IDE`S like eclipse or netbeans or even any editor like edit plus or notepad++ etc. whenever we write any keyword for specified language that keyword changes it`s attributes eg change in color etc.. i`ve tried my best to do that but it`s not working....
here is the code for that stuff .. actually m is an object for frame and t is JTextPane associated with it
it changes the color of selected text but after updating caret the text changes again it`s color..




if you will try this code in a frame after changing some code it will show you what`s happening ...
now i do need a solution for this problem please help me...


scjp 6
http://algorithmsea.blogspot.com
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You should make all updates to the GUI in the Event Dispatcher Thread. You can use EventQueue.invokeAndWait and EventQueue.invokeLater for that, or SwingWorker if you're using Java 6.

A quick attempt:
Disclaimer: not tested.

A SwingWorker executes its doInBackGround method in the background. When publish is called, those objects are gathered and eventually process is called on the Event Dispatcher Thread with all gathered objects so far. At the end, done is called on the Event Dispatcher Thread again.


Also, why do you create a thread if c != ' '? Wouldn't it be more efficient to move the thread creation inside the if-statement?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2171
    
    7
it changes the color of selected text but after updating caret the text changes again it`s color..


Yes, text "selection" is temporary. You need to change the attributes of the text. Read the JTextPane API and follow the "Using Text Components" link for more information. I then suggest you search the web using "syntax highlighting" to find some ideas. What you want to do is not easy.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Each JTextComponent has an optional Highlighter (using JTextComponent.getHighlighter()) you can use for this:
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Just use this and be done with it.

http://code.google.com/p/jsyntaxpane/


GenRocket - Experts at Building Test Data
vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Posts: 65
@
Rob Prime

thanks for your reply...
actually your code is working properly but what do i need is to change the attribute of that selected text then after it should reflect the changes made but what`s happening is that after updating caret the selected text becomes same again ..that is temporary...how to make it(the changes i made) permanent..
is there any class in API to do that ???
i want to highlight selected text with changing it`s color and it should remain permanent after updating caret..

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Have you even read the 3(!) posts above yours?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wriring code for an EDITOR big problem??