How to Change Text Color in the Jtextarea

How to change text color in the JTextArea?

JTextArea is meant to entertain Plain Text. The settings applied to a single character applies to whole of the document in JTextArea. But with JTextPane or JEditorPane you have the choice, to colour your String Literals as per your liking. Here with the help of JTextPane, you can do it like this :

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TextPaneTest extends JFrame
{
private JPanel topPanel;
private JTextPane tPane;

public TextPaneTest()
{
topPanel = new JPanel();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));

tPane = new JTextPane();
tPane.setBorder(eb);
//tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
tPane.setMargin(new Insets(5, 5, 5, 5));

topPanel.add(tPane);

appendToPane(tPane, "My Name is Too Good.\n", Color.RED);
appendToPane(tPane, "I wish I could be ONE of THE BEST on ", Color.BLUE);
appendToPane(tPane, "Stack", Color.DARK_GRAY);
appendToPane(tPane, "Over", Color.MAGENTA);
appendToPane(tPane, "flow", Color.ORANGE);

getContentPane().add(topPanel);

pack();
setVisible(true);
}

private void appendToPane(JTextPane tp, String msg, Color c)
{
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TextPaneTest();
}
});
}
}

here is the Output :

JTextPane

Hightlight and change Color of Text in JTextArea

You can use JComponent#setForeground(Color) - But not on JTextArea. It's not designed for that purpose. Instead, use a JEditorPane or JTextPane.

On these components you can also use HTML tags, like:

"<html>My text: <font color=\"blue\">" + myString + "</font></html>"

You can change the background of the JTextArea using JTextArea#setBackground (If that what you mean).

Formatting Text Color In JTextArea

You need to use a JTextPane for multiple colors.

Check out the Message Console which allows you to redirect messages to the text pane. You can also control the text color for the "err" and "out" messages.

Different text color in a JTextArea

Not with a JTextArea. You can, however, use a JTextPane which allows for attributes to be applied to text. If you want examples, take a look at the Text Component Tutorial.

How can I dynamically change the font colour within a JTextArea?

A JTextArea is only meant to contain plain text and cannot color certain words. If you want to be able to color different words, you need to use a JTextPane or a JEditorPane.

For more information, see this question. This question might also be helpful (especially the second answer).

Here is an example:

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

Style style = textPane.addStyle("Style", null);
StyleConstants.setForeground(style, Color.red);
String word = "Hello";

if (word.equals("Hello")) {
try {
doc.insertString(doc.getLength(), word, style);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
} else {
StyleConstants.setForeground(style, Color.blue);

try {
doc.insertString(doc.getLength(), word, style);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

This makes a String word. If word is "Hello" it will be displayed in red, otherwise it will be displayed in blue.

How to set font color for selected text in jTextArea?

Styled text (with a color attribute for characters) is available as StyledDocument, and usable in JTextPane and JEditorPane. So use a JTextPane.

private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
StyledDocument doc = textPane.getStyledDocument();
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
if (start == end) { // No selection, cursor position.
return;
}
if (start > end) { // Backwards selection?
int life = start;
start = end;
end = life;
}
Style style = textPane.addStyle("MyHilite", null);
StyleConstants.setForeground(style, Color.GREEN.darker());
//style = textPane.getStyle("MyHilite");
doc.setCharacterAttributes(start, end - start, style, false);
}

Mind: the style can be set at the creation of the JTextPane, and as the outcommented code shows, retrieved out of the JTextPane field.

How can I set partial text color in JTextArea?

I'm not sure if JTextArea can be styled in so much detail, since it presumably sets up styles for the whole document from the selected font, color etc. You may have more luck using a JTextPane/JEditorPane.

EDIT: From the javadoc

A JTextArea is a multi-line area that
displays plain text.

(The emphasis is added.)

If you can move to JTextPane, then that will render the formatting.

JTextArea or JTextPane Set Highlighted Text Color

If you mean the "normal" highlight color (when you drag your mouse over the text), this can simply be achieved by

textArea.setSelectionColor(Color.LIGHT_GRAY);

(or whatever color you want it to have.)

If you want to programmatically highlight a character sequence in your text area:

String searchedWord = "word";
int pos1 = textArea.getText().indexOf(searchedWord);
int pos2 = pos1 + searchedWord.length();
try {
textArea.getHighlighter().addHighlight(pos1, pos2,
new DefaultHighlighter.DefaultHighlightPainter(Color.LIGHT_GRAY));
} catch (BadLocationException e) {
e.printStackTrace();
}

(The same works for a JTextPane too)



Related Topics



Leave a reply



Submit