• Post Reply Bookmark Topic Watch Topic
  • New Topic

replace a word in the JTextPane without selecting it

 
rohit rusty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,


using the following code i am able to insert a specific text in the JTextPane.
public void append(Color c, String s) { // better implementation--uses
// StyleContext
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
StyleConstants.Foreground, c);

int len = getDocument().getLength();
setCaretPosition(len); // place caret at the end (with no selection)
setCharacterAttributes(aset, false);
replaceSelection(s); // there is no selection, so inserts at caret
}

}

the append method can be called the followig way
JTextPane pane = new ColorPane();
String s = "replace";
pane.append(Color.red,s);

the problem here is the String s is getting inserted, but i need to replace String s with another occurance of s in the pane. please help.

- rohitrusty314

 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the API for javax.swing.text.Document. You'll find something useful there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!