java programming need help..
on: Friday 27 March, 2009, 11:45:59 PM
on: Friday 27 March, 2009, 11:45:59 PM
hi....
i want to replace a string from a frame`s text area while writing in that text area....
suppose
public class Mymenu extends JFrame
{
String msg="";
Texts t;
//Eventhandler mh;
public Mymenu(String title)
{
super(title);
t=new Texts(this,"");
add(t);
// other stuff
}
if i have to retrieve a string from that text area t and want to replace same string like it happens in some editors ..with changing it`s attribute while writing and carry on.......
how to do that?????
i want to replace a string from a frame`s text area while writing in that text area....
suppose
public class Mymenu extends JFrame
{
String msg="";
Texts t;
//Eventhandler mh;
public Mymenu(String title)
{
super(title);
t=new Texts(this,"");
add(t);
// other stuff
}
if i have to retrieve a string from that text area t and want to replace same string like it happens in some editors ..with changing it`s attribute while writing and carry on.......
how to do that?????
Re: java programming need help..
on: Saturday 28 March, 2009, 04:23:26 AM
on: Saturday 28 March, 2009, 04:23:26 AM
As far as we could infer from your query it meant that you wish to replace a string in a frame's text area.
The solution for this is
[u][b]Step1[/b][/u]: [i]i[/i] = t.getText().indexOf([i]searchString[/i], int [i]start[/i])
This function returns the starting position(an integer [i]i[/i]) of the first occurrence of [i]searchString[/i] from the specified start position(i.e. int [i]start[/i]) in the text area [i]t[/i].
[u][b]Step2[/b][/u]: textArea1.replaceRange(replaceString, int start, int end)
For e.g. the [i]replaceString[/i] = "xyz", [i]searchString[/i] = "abcd" and from the above [b]Step1[/b] we get [i]start[/i] = [i]i[/i], therefore [i]end[/i] = [i]i[/i] + searchString.length() (i.e. in our e.g. [i]i[/i] + 4). Therefore the syntax is "textArea1.replaceRange([i]replaceString, i, i + searchString.length[/i])".
[u][b]Step3[/b][/u]: Now for recursively replacing the [i]searchString[/i] with the [i]replaceString[/i], call the function used in [b]Step1[/b] with [i]start[/i] = [i]i[/i] + replaceString.length() and then call the function used in [b]Step2[/b]. Repeat these steps until you get [i]i[/i] = -1(i.e. the searchString is not found anymore).
For styled text you have to use [i]textPane[/i] instead of [i]textArea[/i].
We hope this is the apt answer for your query.
If you have further problems then please be more specific in explaining your query specially what do you mean by "while writing".
The solution for this is
[u][b]Step1[/b][/u]: [i]i[/i] = t.getText().indexOf([i]searchString[/i], int [i]start[/i])
This function returns the starting position(an integer [i]i[/i]) of the first occurrence of [i]searchString[/i] from the specified start position(i.e. int [i]start[/i]) in the text area [i]t[/i].
[u][b]Step2[/b][/u]: textArea1.replaceRange(replaceString, int start, int end)
For e.g. the [i]replaceString[/i] = "xyz", [i]searchString[/i] = "abcd" and from the above [b]Step1[/b] we get [i]start[/i] = [i]i[/i], therefore [i]end[/i] = [i]i[/i] + searchString.length() (i.e. in our e.g. [i]i[/i] + 4). Therefore the syntax is "textArea1.replaceRange([i]replaceString, i, i + searchString.length[/i])".
[u][b]Step3[/b][/u]: Now for recursively replacing the [i]searchString[/i] with the [i]replaceString[/i], call the function used in [b]Step1[/b] with [i]start[/i] = [i]i[/i] + replaceString.length() and then call the function used in [b]Step2[/b]. Repeat these steps until you get [i]i[/i] = -1(i.e. the searchString is not found anymore).
For styled text you have to use [i]textPane[/i] instead of [i]textArea[/i].
We hope this is the apt answer for your query.
If you have further problems then please be more specific in explaining your query specially what do you mean by "while writing".
Re: java programming need help..
on: Tuesday 07 April, 2009, 12:04:27 PM
on: Tuesday 07 April, 2009, 12:04:27 PM
Actually what i want doesn`t work with textarea... the attribute of selected string doesn`t change .....while writing means :-
like we use eclipse or other editors the format of key words changes while writing.... i wanna do like that....if u have any idea plz tell me...
thanx for ur efforts
like we use eclipse or other editors the format of key words changes while writing.... i wanna do like that....if u have any idea plz tell me...
thanx for ur efforts
Re: java programming need help..
on: Wednesday 08 April, 2009, 07:30:55 AM
on: Wednesday 08 April, 2009, 07:30:55 AM
First of all, if you want to change attributes of selected text then you have to use JTextPane. I am giving you an example of changing an attribute(foreground color):-
[i]MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.RED);
int length = jtextpane.getSelectionEnd() - jtextpane.getSelectionStart();
DefaultStyledDocument dse = (DefaultStyledDocument) jtextpane.getDocument();
dse.setCharacterAttributes(jtextpane.getSelectionStart(), length, mas, false);[/i]
[i]MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.RED);
int length = jtextpane.getSelectionEnd() - jtextpane.getSelectionStart();
DefaultStyledDocument dse = (DefaultStyledDocument) jtextpane.getDocument();
dse.setCharacterAttributes(jtextpane.getSelectionStart(), length, mas, false);[/i]
Re: java programming need help..
on: Monday 22 June, 2009, 11:30:35 PM
on: Monday 22 June, 2009, 11:30:35 PM
here is the code for what am i doing
suppose you type class(or any keyword) in textpane then it should change it`s color(attribute) like it happens when we code in ide`s like eclipse or netbeans or even in some editors like edit plus or notepad++..
but it`s not happening ....
Here in the code : m is JFrame and t is JTextPane associated with it..
if you will run this code in a simple frame with a textpane associated with that
then you will see what`s happening
[code]
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={"class","public","void","static"};
String str="";
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("Dialog",Font.PLAIN,15);
setFont(f);
this.addCaretListener(this);
this.addKeyListener(this);
repaint();
}
public void caretUpdate(CaretEvent ce)
{
int i=this.getCaretPosition();
if(i>0)
{
count=i;
}
}
String[] parse(String text)
{
String[] tokens=text.split("s");
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());
//m.t.setSelectedTextColor(Color.RED);
JLabel label=new JLabel(tok);
//label.setBounds(50, 50, 50, 50);
m.t.insertComponent(label);
//m.t.replaceSelection(new Label(tok).getText());
//m.t.setCaretPosition(caret);
}
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
if(c==' ')
thread.start();
}
}
[/code]
now i need solution for this problem ...
hope all of u can help me..
thanks.
suppose you type class(or any keyword) in textpane then it should change it`s color(attribute) like it happens when we code in ide`s like eclipse or netbeans or even in some editors like edit plus or notepad++..
but it`s not happening ....
Here in the code : m is JFrame and t is JTextPane associated with it..
if you will run this code in a simple frame with a textpane associated with that
then you will see what`s happening
[code]
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={"class","public","void","static"};
String str="";
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("Dialog",Font.PLAIN,15);
setFont(f);
this.addCaretListener(this);
this.addKeyListener(this);
repaint();
}
public void caretUpdate(CaretEvent ce)
{
int i=this.getCaretPosition();
if(i>0)
{
count=i;
}
}
String[] parse(String text)
{
String[] tokens=text.split("s");
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());
//m.t.setSelectedTextColor(Color.RED);
JLabel label=new JLabel(tok);
//label.setBounds(50, 50, 50, 50);
m.t.insertComponent(label);
//m.t.replaceSelection(new Label(tok).getText());
//m.t.setCaretPosition(caret);
}
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
if(c==' ')
thread.start();
}
}
[/code]
now i need solution for this problem ...
hope all of u can help me..
thanks.
Re: java programming need help..
on: Tuesday 23 June, 2009, 10:43:30 AM
on: Tuesday 23 June, 2009, 10:43:30 AM
Whenever a key is typed, search for space from the last character using the function lastIndexOf(). Then check the word using substring function, if it is a keyword then use the last post code to change its color.
[code]
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.RED);
int length = jtextpane.getCaretPosition() - jtextpane.getText().lastIndexOf(" ");
DefaultStyledDocument dse = (DefaultStyledDocument) jtextpane.getDocument();
dse.setCharacterAttributes(jtextpane.getText().lastIndexOf(" "), length, mas, false);
[/code]
[code]
MutableAttributeSet mas = new SimpleAttributeSet();
StyleConstants.setForeground(mas, Color.RED);
int length = jtextpane.getCaretPosition() - jtextpane.getText().lastIndexOf(" ");
DefaultStyledDocument dse = (DefaultStyledDocument) jtextpane.getDocument();
dse.setCharacterAttributes(jtextpane.getText().lastIndexOf(" "), length, mas, false);
[/code]


