Archived

This topic is now archived and is closed to further replies.

ManiacMac

[java] Styled Fonts and Colors

Recommended Posts

ManiacMac    122
Alright, ill explain whats going on. Im working on a project (a chat type thing), that requires the entering of styled text into a Document, thats on a JTextPane. Simple enough, it works fine. Trouble is adding color. Plain text, uses the color you pick fine, but bold, italic, and underline fonts, wont take them, and keep with black. Any ideas as to why it does this (Id like to post the code, but its just this one class (of like 15-20) and is -huge-)?

Share this post


Link to post
Share on other sites
Sully    122
I''m just going to cut and paste a few items from a project I did:


  
void appendText(String text, String colorKey)
{
try
{
SimpleAttributeSet set = config.getAttributeSet(colorKey);
document.insertString(document.getLength(), (text+crlf), set);
systemText.setCaretPosition(document.getLength());
}
catch (BadLocationException ble)
{
}
....

I used a resource file to set the values, so here''s the important parts from that:

  
void buildTextAttributeSet()
{
int count = 0;
StringTokenizer st;
String parms;
while (true)
{
try
{
parms = bundle.getString("TextAttributes_"+count);
}
catch (Exception e)
{
return;
}
st = new StringTokenizer(parms, " ,");
if (st.countTokens() != 12) continue; // malformed parms string

try
{
String key = st.nextToken();
String bold = st.nextToken();
String italic = st.nextToken();
String underline = st.nextToken();
String fontFamily = st.nextToken();
int fontSize = Integer.parseInt(st.nextToken());
int fgRed = Integer.parseInt(st.nextToken());
int fgGreen = Integer.parseInt(st.nextToken());
int fgBlue = Integer.parseInt(st.nextToken());
int bgRed = Integer.parseInt(st.nextToken());
int bgGreen = Integer.parseInt(st.nextToken());
int bgBlue = Integer.parseInt(st.nextToken());

SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, bold.equalsIgnoreCase("true"));
StyleConstants.setItalic(sas, italic.equalsIgnoreCase("true"));
StyleConstants.setUnderline(sas, underline.equalsIgnoreCase("true"));
StyleConstants.setFontFamily(sas, fontFamily);
StyleConstants.setFontSize(sas, fontSize);
StyleConstants.setForeground(sas, new Color(fgRed, fgGreen, fgBlue));
StyleConstants.setBackground(sas, new Color(bgRed, bgGreen, bgBlue));

attributes.put(key, sas);
}
catch (Exception e)
{
// something went wrong, abort this line

}
count++;
}
}

SimpleAttributeSet getAttributeSet(String colorKey)
{
SimpleAttributeSet sas;

if (attributes.containsKey(colorKey))
{
sas = (SimpleAttributeSet)attributes.get(colorKey);
} else
{
sas= new SimpleAttributeSet();
StyleConstants.setForeground(sas, Color.ORANGE);
StyleConstants.setFontFamily(sas, "Courier");
StyleConstants.setFontSize(sas, 14);
}
return sas;
}

And then an example from the resource file would be:


  
#Text Attributes
! key, bold, italic, underline, font family, font size, foreground color, background color
! back/foreground colors are RGB 0-255 int values.
# All fields must be present!

TextAttributes_0 = Join, true, false, false, Courier, 14, 0, 255, 0, 255, 255, 255


Hope that helps,
-Ron

Share this post


Link to post
Share on other sites