Nadam se da sam shvatio pitanje, ti hoces da imas novi JFrame i u njemu text field gdje je svaka rijec napisana drugom bojom. Ako to hoces da uradis, moraces upotrijebiti jedan od Java Styled Text Area komponenata (npr. JTextPane). JTextArea nece raditi jer kad u njemu promjenis boju promjenice ti se boja cijelog teksta. Ispod imas prijmer kako da ovo uradis sa JTextPane. Svaki put kad dodajes text u StyledDocument, moras mu dodati novi stil (npr. promjeniti boju).
Code:
public void addToSentMessageHistory(String sentMessage)
{
StyledDocument doc = (StyledDocument)clientMessageHistoryArea.getDocument();
try
{
// Create a style object and then set the style attributes
Style style = doc.addStyle("StyleName", null);
// Font size
StyleConstants.setFontSize(style, 15);
// Set text style preferences
// Italic
StyleConstants.setItalic(style, true);
if(count % 2 == 0)
// Bold
StyleConstants.setBold(style, true);
// Font family
StyleConstants.setFontFamily(style, "SansSerif");
// Background color
StyleConstants.setBackground(style, Color.yellow);
// Foreground color
StyleConstants.setForeground(style, Color.white);
doc.insertString(doc.getLength(), sentMessage, style);
}
catch(BadLocationException e)
{
System.out.println("ClientFrame addToReceivedMessageHistory() exception: " + e.getMessage());
}
clientMessageHistoryArea.setCaretPosition(clientMessageHistoryArea.getDocument().getLength());
}