Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Colored text in a JTextArea?

Thread view: 
Inertia_sublimation - 16 May 2004 21:38 GMT
Hello everyone!

I'm wondering where Swing provides the facility to color text in a text area. 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.

I would greatly appreciate an explanation of how to do something like this.

Thanks in advance!
Joe Vasher - 16 May 2004 21:47 GMT
> Hello everyone!
>
[quoted text clipped - 5 lines]
>
> Thanks in advance!

I think you can use

 JTextArea textArea = new JTextArea();

         textArea.setForgroundColor( Color.RED );
Andrew Thompson - 16 May 2004 21:51 GMT
> 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..."


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.