Setting color of a specific line?

okay so in swing is it possible to set a portion of the text in a text field to a different color? Like in my chat program(below) can I make it so all incoming chat(instring) is blue and all outgoing text(string) is red?

Am I using the wrong text component for changing the color of a line? Should I even be using J text components for this?

The tutorial often puts tons of different text components in to its examples, and uses odd methods to change the color.

It seems that I have to use something like setCaretColor, but i am not clear on how to use this in the way I want to use it.

here is the code:

import java.io.*;

import java.net.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.text.*;

import javax.swing.JOptionPane;

import javax.swing.JDialog;

import javax.swing.JTextField;

import java.beans.*;//property change stuff

import java.awt.*;

import java.awt.event.*;

publicclass serverextends JFrameimplements ActionListener

{

JButton recieve;

String string;

String instring;

String aft =" ";

String bef =" ";

String allchat=" ";

JTextField towrite;

boolean stop =false;

String name;

JTextArea chatbox;

public server(){

setSize(490,550);

getContentPane().setLayout(null);

recieve =new JButton("send message");

recieve.setBounds(30,150,150,35);

getContentPane().add(recieve);

recieve.addActionListener(this);

chatbox =new JTextArea("chat goes here");

chatbox.setEditable(false);

chatbox.setBounds(10,200,460,270);

getContentPane().add(chatbox);

towrite =new JTextField("Write message here");

towrite.setBounds(10,475,460,35);

getContentPane().add(towrite);

towrite.addActionListener(this);

setVisible(true);

name = (String)JOptionPane.showInputDialog(null,"Enter your name");

while(stop==false){

try

{

ServerSocket socket =new ServerSocket(4019);

Socket insocket =socket.accept();

BufferedReader in =new BufferedReader (new InputStreamReader(insocket.getInputStream()));

instring = in.readLine();

socket.close();

bef = instring;

if(bef.equals(aft)){}else{

allchat=instring+"\n"+allchat;

chatbox.setText(allchat);

}

}

catch(Exception e){}

}

}

publicvoid actionPerformed(ActionEvent evt)

{

if(evt.getActionCommand().equals("send message"))

{

try

{

Socket socket =new Socket("67.166.126.246",4020);

OutputStream out = socket.getOutputStream();

string=name+": "+towrite.getText();

byte buffer[] = string.getBytes();

out.write(buffer);

socket.close();

}

catch(Exception e){}

bef = string;

if(bef.equals(aft)){}else{

allchat=string+"\n"+allchat;

chatbox.setText(allchat);

}

aft=bef;

}

}

publicstaticvoid main(String[] args)

{

server s =new server();}}

[6280 byte] By [Robina] at [2007-10-3 2:47:37]
# 1
you can use JTextPane which supports HTML formatting but its a little heavy component and also it does not have an append method so you will have to set the text again and again.
LRMKa at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 2
Swing related questions should be posted in the Swing forum.I've posted a class, TextPaneAttributes, in the forum that shows how to color different pieces of text.
camickra at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 3

Hmmmm im sorry but Im not quite sure I understand how to do this.

Do you have to know the exact digits at which the text starts and ends?

or can you, as mentioned just use html tags to set the text color?

If i were to highlight a line of the text, would that text be highlighted even if its location in the string changed?

It would seem that tags would be easier but from what I have tried they don't seem to work in editor panes, but the ydo work in buttons

Im sorry, Im being a bit of a pest, but I have not been doing java for that long, and I do not have a teacher, so all of my questions have to go here.

Robina at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 4

> you can use JTextPane which supports HTML formatting

If you want to use HTML then you should use JEditorPane.

> and also it does not have an append method

You use the insertString(...) method of the Document.

> so you will have to set the text again and again.

very inefficient, see above.

camickra at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 5

Hmmmmmm so how would I use html in the editor pane?

Heres what I am trying, but it spells out the html tags instead of doing what they are supposed to do:

chatbox = new JEditorPane();

chatbox.setEditable(false);

chatbox.setBounds(10,200,460,270);

getContentPane().add(chatbox);

chatbox.setText("<html><center><u>this text should be underlined</u>");

Robina at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 6

> Hmmmmmm so how would I use html in the editor pane?

You could start by reading the JEditorPane API which has a link that takes you to the Swing tutorial that has a working example of loading an HTML file into a JEditorPane. Works fine for displaying HTML files, but I've not had a lot of success modify the existing HTML, which is why I gave you an alternative approach that allows you to set the attributes of the text directly, either to existing text or newly added text.

camickra at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 7
really sorry, but I still have a few more questions...So, now I think I will use several labels going down the screen. Labels can be edited by html. Is there a way to set the color of the label using RGB instead of html tags?
Robina at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...
# 8
It's a lot easier to find that info in the documentation when you know you aren't setting the "colour", you are setting the "background". There's a setBackground(Color) method.
DrClapa at 2007-7-14 20:36:24 > top of Java-index,Java Essentials,Java Programming...