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();}}

