<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[JavaRanch: Latest posts for the topic "wriring code for an EDITOR  big problem??"]]></title>
		<link>http://www.coderanch.com/forums/t/2/Swing-AWT-SWT-JFace/wriring-code-EDITOR-big</link>
		<description><![CDATA[Latest messages posted in the topic "wriring code for an EDITOR  big problem??"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[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.... <img src="http://www.coderanch.com/images/smilies/2786c5c8e1a8be796fb2f726cca5a0fe.gif" /> <br /> here is the code for that stuff .. actually m is an object for frame and t is <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html" class="api" title="Java API" target="_new" rel="nofollow">JTextPane</a> associated with it <br /> it changes the color of selected text but after updating caret the text  changes  again it`s color.. <img src="http://www.coderanch.com/images/smilies/jr-confused.gif" border="0"> <br /> <br /> <br /> <pre> import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.rtf.*;

public class EditorPane extends JTextPane implements CaretListener,KeyListener
{
	private String[] KEY_WORDS={&quot;class&quot;,&quot;public&quot;,&quot;void&quot;,&quot;static&quot;};
	String str=&quot;&quot;;
	Font f;
	Mymenu m;
	protected RTFEditorKit m_kit;
	static int count=0;
	static int edit=0;
	
	EditorPane()
	{
		
	}
	EditorPane(Mymenu m,String text)
	{
		super();
		this.setText(text);
		this.m_kit=new RTFEditorKit();
		this.m=m;
		//this.setEditorKit(this.m_kit);
		
		JScrollPane ps = new JScrollPane(this);
		//m.getContentPane().add(ps, BorderLayout.CENTER);
		
		m.getContentPane().add(ps);		//not working,,...
		
		
		this.str=this.getText();
		f=new Font(&quot;Dialog&quot;,Font.PLAIN,15);
		setFont(f);
		
		this.addCaretListener(this);
		
		this.addKeyListener(this);
		repaint();
		
	}
	
	

	public void caretUpdate(CaretEvent ce) 
	{
		int i=this.getCaretPosition();
		if(i&gt;0)
		{
			count=i;
		}
	}
	
	String[] parse(String text)
	{
		String[] tokens=text.split(&quot;\\s&quot;);
		return tokens;
	}
	
	
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void keyTyped(KeyEvent ke) 
	{
	   char c=ke.getKeyChar();
	   ke.getKeyLocation();
	  
		Thread thread=new Thread(){
			public void run()
			{
				EditorPane obj=new EditorPane();
				try
				{
					String text=m.t.getText();
					String[] tokens=obj.parse(text);
					int i=0;
					for(String tok:tokens)
					{
						int j=i;
						i=i+tok.length();
						for(String KEY_WORD:obj.KEY_WORDS)
						{
							if(tok.equals(KEY_WORD))
							{
								int caret=m.t.getCaretPosition();
								m.t.select(j, i+1);
								System.out.println(m.t.getSelectedText());
								//Font f=new Font(Font.DIALOG,Font.BOLD,20);
								m.t.setSelectedTextColor(Color.RED);
								
							}
						}
					}
					
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
			}
		};
		if(c==' ')
			thread.start();
		
	}

}
   </pre><br /> <br /> if you will try this code in a frame after changing some code it will show you what`s happening ...<br /> now i do need a solution for this problem please help me...]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2007176</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2007176</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 21:41:35 MDT]]></pubDate>
				<author><![CDATA[vikky agrawal]]></author>
			</item>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[You should make all updates to the GUI in the Event Dispatcher Thread. You can use EventQueue.invokeAndWait and EventQueue.invokeLater for that, or <a href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html" class="api" title="Java API" target="_new" rel="nofollow">SwingWorker</a> if you're using <a href="http://www.javaranch.com" class="faq" title="A Friendly Place for Java Greenhorns" target="_new">Java</a> 6.<br /> <br /> A quick attempt:<br /> <pre>public void keyTyped(KeyEvent ke)    
{   
    char c = ke.getKeyChar();   
    ke.getKeyLocation();   
    if (c == ' ') // perhaps change this to Character.isWhitespace(c) to also handle tabs and enters
    {
        class Selection
        {
            int start;
            int end;
        }

        final String text = m.t.getText();   
        SwingWorker&lt;Void,Selection&gt; worker = new SwingWorker&lt;Void,Selection&gt;()
        {
            @Override
            protected Void doInBackGround()
            {
                String[] tokens = parse(text); // use this instance!
                int i=0;   
                for(String tok: tokens)   
                {   
                    int j=i;   
                    i = i + tok.length();   
                    for (String KEY_WORD: KEY_WORDS) // again, this instance
                    {   
                        if (tok.equals(KEY_WORD))   
                        {   
                            Selection selection = new Selection();
                            selection.start = j;
                            selection.end = i + 1;
                            publish(selection);
                        }   
                    }   
                }   
                return null;
            }

            @Override
            protected void process(List&lt;Selection&gt; chunks)
            {
                for (Selection selection: chunks)
                {
                    int caret = m.t.getCaretPosition();   
                    m.t.select(selection.start, selection.end);
                    System.out.println(m.t.getSelectedText());   
                    //Font f=new Font(Font.DIALOG,Font.BOLD,20);   
                    m.t.setSelectedTextColor(Color.RED);   
                }
            }
        };
        worker.execute();
    }
}</pre><span style="font-size: 7px; line-height: normal;">Disclaimer: not tested.</span><br /> <br /> A <a href="http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html" class="api" title="Java API" target="_new" rel="nofollow">SwingWorker</a> 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.<br /> <br /> <br /> Also, why do you create a thread if c != ' '? Wouldn't it be more efficient to move the thread creation inside the if-statement?<br /> <pre>if(c==' ')
{
    Thread thread = ...
    thread.start();   
}</pre>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2007339</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2007339</link>
				<pubDate><![CDATA[Tue, Jun 23 2009 01:33:06 MDT]]></pubDate>
				<author><![CDATA[Rob Prime]]></author>
			</item>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[<blockquote class="uncited">
			<div>it changes the color of selected text but after updating caret the text changes again it`s color.. </div>
		</blockquote><br /> <br /> Yes, text "selection" is temporary. You need to change the attributes of the text. Read the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html" class="api" title="Java API" target="_new" rel="nofollow">JTextPane</a> 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. ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2007810</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2007810</link>
				<pubDate><![CDATA[Tue, Jun 23 2009 09:19:37 MDT]]></pubDate>
				<author><![CDATA[Rob Camick]]></author>
			</item>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[Each <a href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html" class="api" title="Java API" target="_new" rel="nofollow">JTextComponent</a> has an optional Highlighter (using JTextComponent.getHighlighter()) you can use for this:<br /> <pre>Highlighter highlighter = getHighlighter();
if (highlighter == null)
{
    highlighter = new DefaultHighlighter();
    setHighlighter(highlighter);
}
// use highlighter.addHighlight(...)</pre>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2007946</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2007946</link>
				<pubDate><![CDATA[Tue, Jun 23 2009 12:33:15 MDT]]></pubDate>
				<author><![CDATA[Rob Prime]]></author>
			</item>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[Just use this and be done with it.<br /> <br /> <a class="snap_shots" href="http://code.google.com/p/jsyntaxpane/" target="_blank" rel="nofollow">http://code.google.com/p/jsyntaxpane/</a>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2007954</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2007954</link>
				<pubDate><![CDATA[Tue, Jun 23 2009 12:44:29 MDT]]></pubDate>
				<author><![CDATA[Gregg Bolinger]]></author>
			</item>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[<b>@</b><br /> <b>Rob Prime</b><br /> <br /> thanks for your reply...<br /> 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..<br /> is there any class in API to do that ???<br /> i want to highlight selected text with changing it`s color and it should remain permanent after updating caret..<br /> <br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2007969</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2007969</link>
				<pubDate><![CDATA[Tue, Jun 23 2009 13:00:21 MDT]]></pubDate>
				<author><![CDATA[vikky agrawal]]></author>
			</item>
			<item>
				<title>wriring code for an EDITOR  big problem??</title>
				<description><![CDATA[Have you even read the 3(!) posts above yours?]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450851/2008030</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450851/2008030</link>
				<pubDate><![CDATA[Tue, Jun 23 2009 14:41:14 MDT]]></pubDate>
				<author><![CDATA[Rob Prime]]></author>
			</item>
	</channel>
</rss>
