I'm currently working on a old project that was given to me, it currently use java swing and has a basic gui. It has a ColorPane that extends Jtextpane to change colors of selected text.
It uses this methond
public void changeSelectedColor(Color c) {
changeTextAtPosition(c, this.getSelectionStart(), this.getSelectionEnd());
}
Say that string = "Hello World!" Hello color is green World is black. How do I grab Hello out base on its color from the Jtextpane. I've tried the clunky way which is just storing the selected word as I change there color but is there a way where I can grab all the green text in one go? I've tried googling but...it hasn't really came up with any good methods. Can anyone point me in the right direction?
There's probably a number of ways to do this, but...
You need to get a reference to the
StyleDocument
that is backing theJTextPane
, starting at a given character position, you need to check the characters attributes for the given color, iftrue
, continue onto the text character, else you're done.This example simple finds the first word that is colored red, but you could just as easily walk the entire document and find all the words you want...