Forums Register Login

replace a word in the JTextPane without selecting it

+Pie Number of slices to send: Send
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

1
+Pie Number of slices to send: Send
Check out the API for javax.swing.text.Document. You'll find something useful there.
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1703 times.
Similar Threads
comboBox problem need help bad
replaceSelection() in TextPane
Running errors ins MyMusic.java program
Create Swing Program
JTable Cell Renderers
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:53:20.