> Hello everyone!
>
[quoted text clipped - 5 lines]
>
> Thanks in advance!
I think you can use
JTextArea textArea = new JTextArea();
textArea.setForgroundColor( Color.RED );
> I'm wondering where Swing provides the facility to color text in a text area.
No. Well ..yes maybe if you fiddled with
its default font, but it is not really designed
for styled text.
>...I saw something about RTF in the docs,
> but Im not too familiar with either RTF or
> how/where Swing parses it to color the text.
For RTF/HTML etc. you can use a JEDitorPane
<http://www.physci.org/api.jsp?class=javax.swing.JEditorPane>
[ Ignore the first link and go to the JavaDocs.. ]
Here is a little example of rendering HTML using JEditorPane.
<http://www.physci.org/launcher.jsp?class=/codes/HTMLOP/JAddPane>
Well.. there are no colors used, but if you
can get colored text in HTML, you can then do
that in a Swing component and it will render
as HTML.
HTH

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Fahd Shariff - 17 May 2004 10:14 GMT
A JTextPane would be more suitable. A jtextpane has a styled
document and using this, along with StyleConstants and an
AttributeSet, you can apply any style to your text. (Check out the
javadocs for the above classes)
A simple example that i hacked together is shown below:
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.text.* ;
public class MyProgram extends JFrame
{
JTextPane editor ;
public MyProgram()
{
super() ;
editor = new JTextPane() ;
getContentPane().add(editor,BorderLayout.CENTER) ;
setVisible(true) ;
try{
writeSomeText() ;
}catch(Exception e){e.printStackTrace() ;}
}
//write some multicolored text
public void writeSomeText() throws Exception
{
MutableAttributeSet attr = new SimpleAttributeSet();
StyledDocument doc = editor.getStyledDocument();
//write some text in red
StyleConstants.setForeground(attr, Color.red);
int offset = doc.getLength();
doc.insertString(offset, "HELLO ", attr) ;
//bold text with yellow bg, blue fg
StyleConstants.setForeground(attr, Color.blue);
StyleConstants.setBackground(attr, Color.yellow);
StyleConstants.setBold(attr,true);
offset = doc.getLength();
doc.insertString(offset, "World! ", attr) ;
}
public static void main(String[] args)
{
new MyProgram() ;
}
}
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."
Fahd Shariff - 17 May 2004 10:18 GMT
You can use a JTextPane to add color to text. A jtextpane has a styled
document and using this, along with StyleConstants and an
AttributeSet, you can apply any style to your text. (Check out the
javadocs for the above classes)
A simple example that i hacked together is shown below:
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.text.* ;
public class MyProgram extends JFrame
{
JTextPane editor ;
public MyProgram()
{
super() ;
editor = new JTextPane() ;
getContentPane().add(editor,BorderLayout.CENTER) ;
setVisible(true) ;
try{
writeSomeText() ;
}catch(Exception e){e.printStackTrace() ;}
}
//write some multicolored text
public void writeSomeText() throws Exception
{
MutableAttributeSet attr = new SimpleAttributeSet();
StyledDocument doc = editor.getStyledDocument();
//write some text in red
StyleConstants.setForeground(attr, Color.red);
int offset = doc.getLength();
doc.insertString(offset, "HELLO ", attr) ;
//bold text with yellow bg, blue fg
StyleConstants.setForeground(attr, Color.blue);
StyleConstants.setBackground(attr, Color.yellow);
StyleConstants.setBold(attr,true);
offset = doc.getLength();
doc.insertString(offset, "World! ", attr) ;
}
public static void main(String[] args)
{
new MyProgram() ;
}
}
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."