Alternate Display Font Colour

I have this short code

import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.JTextArea;

publicclass Mainextends JFrame

{

public Main()

{

super("Colour Alternator");

alternate();

}

/**

* Creates the JTextArea with alternating colours

*/

publicvoid alternate()

{

setDefaultCloseOperation(EXIT_ON_CLOSE);

final JTextArea alternator =new JTextArea("This is where the text is displayed\n");

alternator.append("Hello World\n");

alternator.setForeground(Color.RED);

alternator.append("Hello World\n");

alternator.setForeground(Color.GREEN);

alternator.append("Hello World\n");

alternator.setForeground(Color.YELLOW);

alternator.append("Hello World\n");

alternator.setForeground(Color.BLACK);

alternator.append("Hello World\n");

alternator.setForeground(Color.BLUE);

alternator.append("Hello World\n");

setLayout(new BorderLayout());

getContentPane().add(alternator, BorderLayout.CENTER);

setBounds(100, 100, 500, 500);

setVisible(true);

}

publicstaticvoid main(String[] args)

{

new Main();

}

}

which the JtextArea displays with the last colour I have specified. I however, would like it to alternate the colour. Anyone?

[2476 byte] By [Jamwaa] at [2007-11-27 8:14:14]
# 1
You can't use a JTextArea for this. You need to use a JTextPane and set the attributes for each row. This posting has a simple example: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=342068
camickra at 2007-7-12 19:58:45 > top of Java-index,Desktop,Core GUI APIs...