• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wriring code for an EDITOR big problem??

 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Camick
Ranch Hand
Posts: 2536
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each JTextComponent has an optional Highlighter (using JTextComponent.getHighlighter()) you can use for this:
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use this and be done with it.

http://code.google.com/p/jsyntaxpane/
 
vikky agrawal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@
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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you even read the 3(!) posts above yours?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic