[java] Styled Fonts and Colors

Started by
2 comments, last by ManiacMac 20 years, 11 months ago
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-)?
Advertisement
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
Thank you, ill spend more time going over it when I get home. Thanks.
In the jdk directory, under demos/jfc/StyledText, check out the source for the StyledText demo. It does exactly what you want.

This topic is closed to new replies.

Advertisement