The most intelligent Java IDE
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
Author

wriring code for an EDITOR big problem??

vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Messages: 47

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...

This message was edited 1 time. Last update was at by vikky agrawal


scjp 6
Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8771

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
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Messages: 675

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 Prime
Bartender

Joined: Oct 27, 2005
Messages: 8771

Each JTextComponent has an optional Highlighter (using JTextComponent.getHighlighter()) you can use for this:

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Messages: 13661

Just use this and be done with it.

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

"Fookin Prawns"
vikky agrawal
Ranch Hand

Joined: Dec 18, 2008
Messages: 47

@
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..


scjp 6
Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8771

Have you even read the 3(!) posts above yours?

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
hibernate profiler

.