Unable To Change Color In JTextArea

B

Brett Sheeran

I wonder if antone can tell me why the following snippit of code does not
change the text color to red.

private JTextArea jTextArea1 = new JTextArea();
....
//in jbInit
this.getContentPane().add(jTextArea1, BorderLayout.CENTER);
....
jTextArea1.setText("A whole bunch of text in here...");
jTextArea1.select(5, 15);
jTextArea1.setSelectedTextColor(Color.red);
jTextArea1.repaint();

Thanks.

Regards Brett Sheeran
 
M

M Verkerk

This method changes the color of SELECTED text in your textarea,
use setForeground(Color.red) instead..
 
M

Marius

Brett Sheeran said:
I wonder if antone can tell me why the following snippit of code does not
change the text color to red.

private JTextArea jTextArea1 = new JTextArea();
...
//in jbInit
this.getContentPane().add(jTextArea1, BorderLayout.CENTER);
...
jTextArea1.setText("A whole bunch of text in here...");
jTextArea1.select(5, 15);
jTextArea1.setSelectedTextColor(Color.red);
jTextArea1.repaint();

Thanks.

Regards Brett Sheeran

I tried your code and it worked - here is what I did...

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class GoogleTest extends JPanel {

public GoogleTest(){
this.setLayout(new BorderLayout());
JTextArea jTextArea1 = new JTextArea();
add(jTextArea1, BorderLayout.CENTER);
jTextArea1.setText("A whole bunch of text in here...");
jTextArea1.select(5, 15);
jTextArea1.setSelectedTextColor(Color.red);
jTextArea1.repaint();
}

public static void main(String[] args) {
JFrame frame = new JFrame();
GoogleTest panel = new GoogleTest();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
 
Joined
Jul 4, 2006
Messages
1
Reaction score
0
Hi, I'm new at Java, have been reading some books allready. Got this question :
Is it possible to add text to JTextArea with different colors like :
text 1
text 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top