dcsimg
About font color
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   A_KM
Posted On:   Thursday, March 20, 2003 02:44 AM

Hi!
I want my JTextArea to show strings in different colors, so I want to change Font color. I tried to use Font constructor Font(Map), where I put TextAttribute.FOREGROUND key and Color.blue value, but it doestn't work. Why is that? May be it's the wrong component (JTextArea), which doesn't support multicoloring?
Thank you.

Re: About font color

Posted By:   dharmendra_dubey  
Posted On:   Sunday, May 18, 2003 10:09 PM

if the document type of youe text area is html type the just put the text in html ... with whtever color you want (by using ) tag ,



m_txaHTML = new JTextArea(8,50);
m_txaHTML.setEditable(false);
m_txaHTML.setLineWrap(true);
m_scpHTML = new JScrollPane(m_txaHTML);
m_txpEditor = new JTextPane();
m_scpEditor = new JScrollPane(m_txpEditor);
m_scpEditor.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
m_scpEditor.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
m_txpEditor.setEditable(false);
m_txpEditor.setBackground(EDITOR_BACKGROUND);
//m_txaHTML.setSize(330,180);
m_txaHTML.setPreferredSize(new Dimension(240,250));
//m_scpHTML.setSize(330,180);
m_scpHTML.setPreferredSize(new Dimension(240,250));
//m_scpEditor.setSize(330,180);
m_scpEditor.setPreferredSize(new Dimension(240,250));

===================================================

Function code which set the editor type of the text area



String testHtml=txtStr;
javax.swing.text.html.HTMLEditorKit htmlEditorKit = new javax.swing.text.html.HTMLEditorKit();
javax.swing.text.html.HTMLDocument htmlDoc = (javax.swing.text.html.HTMLDocument)htmlEditorKit.createDefaultDocument();
m_txpEditor.setEditorKit(htmlEditorKit);
m_txpEditor.setDocument(htmlDoc);

StringReader sr=new StringReader(testHtml);
htmlEditorKit.read(sr,htmlDoc,0);

Re: About font color

Posted By:   Bahman_Barzideh  
Posted On:   Thursday, March 20, 2003 04:59 AM

JTextArea is monochrome. It supports one text (foreground) color on one background color.
If you need multi-color text (different text segments have different
foreground/background color) you should look into JTextPane or its parent JEditorPane.

About | Sitemap | Contact