thanks for reply
i have used JTextArea with textArea.append.text() command to get the data on text area but there is an error message on append.text. here is the code plz tell me that am i using a wrong command or some thing else . plz help me soon.
/**
* Chat Applet (Graphical chat client)
* -----------------------------------
* java ChatApplet [host-name] //default localhost:6666
*
* by Huma Munir Talpur (
[email protected])
*
* for JDK 1.0.2 and above
*
*/
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
public class ChatApplet extends Frame implements Runnable
{
static String command; //shared resource
Socket socket;
static final int portNumber=1666;
//Thread commReader=null;
DataInputStream input;
PrintStream output;
String alias=null;
boolean logging=true;
//TextArea textArea;
//TextField comm;
//Label status;
JTextArea textArea;
JTextField comm;
JLabel status;
public static void main(String[] argv) throws IOException
{
InetAddress clientAddr;
if(argv.length==0)
clientAddr=InetAddress.getLocalHost();
else
clientAddr=InetAddress.getByName(argv[0]);
System.out.println("Connecting to Chat Server at "+clientAddr);
ChatApplet ca=new ChatApplet(clientAddr);
// ca.show();
Thread t=new Thread(ca);
t.start();
}
public ChatApplet(InetAddress adx)
{
super(" Welcome To Induljin Chat");
//AWT
resize(300,400);
//setLayout(new BorderLayout());
//textArea=new TextArea(20,20);
//textArea.setEditable(false);
//comm=new TextField(30);
//status=new Label("Please type usser name to Connect..");
//add("North",comm);
//add("Center",textArea);
//add("South",status);
textArea=new JTextArea(20,20);
//textArea.setEditable(false);
comm=new JTextField(30);
status=new JLabel("Please type usser name to Connect..");
getContentPane().add(comm,BorderLayout.NORTH);
getContentPane().add(textArea,BorderLayout.CENTER) ;
getContentPane().add(status,BorderLayout.SOUTH);
//CommandReader cr=new CommandReader(this);
//commReader=new Thread(cr);
command="xXx";
try
{
socket=new Socket(adx,portNumber);
input=new DataInputStream(socket.getInputStream());
output=new PrintStream(socket.getOutputStream());
//commReader.start();
}
catch(IOException e)
{
status.setText("Abnormal chat client socket condition:"+e);
repaint();
}
}
public boolean action(Event e,Object o)
{
if(e.target==comm)
{
synchronized(command)
{
command=comm.getText();
comm.setText("");
}
return true;
}
return false;
}
public boolean handleEvent(Event evt)
{
if (evt.id == Event.WINDOW_DESTROY)
{
synchronized(command)
{
command="exit";
}
}
/*if (evt.id == Event.ACTION_EVENT)
{
String text = textField.getText();
textArea.appendText(text + "\n");
textField.selectAll();
} */
return super.handleEvent(evt);
}
public void run()
{
show();
try
{
doLoop();
input.close();
output.close();
socket.close();
System.exit(0);
}
catch(IOException e)
{
status.setText("Abnormal chat client socket condition:"+e);;
repaint();
}
}
public void doLoop() throws IOException
{
String nullString=new String("xXx");
String data=nullString;
while(true)
{
synchronized(command)
{
//System.out.println("Leggo synch");
if(!command.equals("xXx"))
{
data=command;
//reset
command="xXx";
//System.out.println("Leggo e prendo");
}
} //end synch
if(!data.equals("xXx"))
{
if(logging)
{
//il primo messaggio
logging=false;
alias=data;
status.setText("Logging as:"+alias);
repaint();
output.println("S"+alias); //Starting new chat
}
else
{
status.setText("Sending msg:"+data);
textArea.setEditable(true);
textArea.appendText(">"+data+"\n");
repaint();
output.println("D"+data); //sending data
}
if(data.equals("exit"))
{
status.setText("Exiting Chat...");
repaint();
output.println("X"+alias);
break;
}
}
//System.out.println("read socket");
data=input.readLine();
if(!data.equals("xXx"))
{
//System.out.println(data);
textArea.appendText("<"+data+"\n");
repaint();
}
data=nullString;
}
}
}
HumaMunir