Question

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?

Was it helpful?

Solution

There's probably a number of ways to do this, but...

You need to get a reference to the StyleDocument that is backing the JTextPane, starting at a given character position, you need to check the characters attributes for the given color, if true, continue onto the text character, else you're done.

import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Scrape {

    public static void main(String[] args) {
        JTextPane textPane = new JTextPane();
        StyledDocument doc = textPane.getStyledDocument();

        Style style = textPane.addStyle("I'm a Style", null);
        StyleConstants.setForeground(style, Color.red);

        try {
            doc.insertString(doc.getLength(), "BLAH ", style);
        } catch (BadLocationException ex) {
        }

        StyleConstants.setForeground(style, Color.blue);

        try {
            doc.insertString(doc.getLength(), "BLEH", style);
        } catch (BadLocationException e) {
        }

        Color color = null;
        int startIndex = 0;
        do {
            Element element = doc.getCharacterElement(startIndex);
            color = doc.getForeground(element.getAttributes());
            startIndex++;
        } while (!color.equals(Color.RED));
        startIndex--;

        if (startIndex >= 0) {

            int endIndex = startIndex;
            do {
                Element element = doc.getCharacterElement(endIndex);
                color = doc.getForeground(element.getAttributes());
                endIndex++;
            } while (color.equals(Color.RED));
            endIndex--;
            if (endIndex > startIndex) {
                try {
                    String text = doc.getText(startIndex, endIndex);
                    System.out.println("Red text = " + text);
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            } else {
                System.out.println("Not Found");
            }
        } else {
            System.out.println("Not Found");
        }
    }
}

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top