0

I have sixteen character pairs (e.g. §c, each code starts with § and is followed with 0-9 or a-f) that represent a java.awt.Color. Here's two examples:

  1. §a is Color.getHSBColor(0.333f, 1.000f, 1.000f);
  2. §6 is Color.getHSBColor(0.167f, 1.000f, 0.502f);

I need a way to set the color of a String to any combination of these colors.

Example: §aThis text is green, but §6this text is gold.
Formatted: <green>This text is green, but <gold>this text is gold

Note that the <> tags were just examples, I need to use the following method to actually set the color in a JTextPane:

public void append(Color c, String s)
{
    try
    {
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setForeground(sas, c);

        this.getDocument().insertString(getDocument().getLength(), s, sas);
    }
    catch (BadLocationException ex) { ex.printStackTrace();}
}

So far no methods of mine have worked. I've seen several close questions, but none relating to changing text depending on 'color codes'.

Tl;dr - I need a method for programmatically inserting formatted text into a JTextPane using color codes.

  • 2
    [So far no methods of mine have worked. I've seen several close questions, but none relating to changing text depending on 'color codes'. - e.g. can't see diff.](http://stackoverflow.com/a/9651404/714968) – mKorbel May 31 '15 at 07:03
  • @mKorbel I see that works off using String positions. That could work, because I could use a loop to identify areas 'needing' a color change. I'll try it out in the morning, and update my post with the results. – Hydroxocobalamin May 31 '15 at 07:13
  • Just get the positions of the color codes and apply the color attribute, see [all of these](https://www.google.co.il/search?client=opera&q=change+color+of+text+in+jtextpane&sourceid=opera&ie=UTF-8&oe=UTF-8#q=change+color+of+text+in+jtextpane+stackoverflow). – user1803551 May 31 '15 at 08:17

0 Answers0