I created a multi user chat system using sockets in java . everything
works perfect but a little issue. the common jtextarea where i am
supposed to display the chats ( its a app wherein all the users write messages and the message of all the users is visible in their chat
space. Unfortunately the jtextarea is not updating itself whenever a
client writes a message . I tried doing like
System.out.print(contents of the common textarea) . In the command
prompt every single message written is displayed , which means the
code is working as intended . its just the textarea is not updating
itself. i searched many forums even tried doing the update thing in a different thread than EDT but still i have the same problem. Can
anyone help me out ? Here is the code .
public void run() { try { DataInputStream in = new DataInputStream(client.getInputStream()); //client is the socket
object clientName = in.readUTF();
//clientname is a string which stores the name of the clients
s = "Joined"; SwingUtilities.invokeLater(new Runnable(){ public void
run() { clientChat.ta.append(clientName+": "+s); //clientchat
is the name of the Client class and ta is the common textarea object
which is declared as a static variable } });
while(!s.equals("terminate")) { s=in.readUTF(); SwingUtilities.invokeLater(new Runnable(){ public void run() { clientChat.ta.append(clientName+": "+s); } }); }
}catch(Exception e){e.printStackTrace();} }
every time a new client joins a new thread is created that acts as a
server for the client. this is the class where the updation of the
textarea is taking place. any help would be greatly praised.
clientChat.ta is the common textarea for appending the messages.
Knute i thank you for your interest in solving my issue. What my
application does is i have created a class in which a loop
continously listens for connection requests. Another one that i
posted in my previous comment is the chatServerThread class that
allocates a new thread to each request. And then the last class is
the client chat window. What i have done is that i have created a
common textarea in which all the users write their messages. And i
have included that JTextarea ( JSwing ) in all of the client chat
window. But that textarea is not updating ehen i write a message from
any one of the clients window. If you order me i would post the
entire code here. Please help me out .
Knute i thank you for your interest in solving my issue. What my
application does is i have created a class in which a loop
continously listens for connection requests. Another one that i
posted in my previous comment is the chatServerThread class that
allocates a new thread to each request. And then the last class is
the client chat window. What i have done is that i have created a
common textarea in which all the users write their messages. And i
have included that JTextarea ( JSwing ) in all of the client chat
window. But that textarea is not updating ehen i write a message from
any one of the clients window. If you order me i would post the
entire code here. Please help me out .
On 12/14/2016 10:47, abhprk3926@gmail.com wrote:
clientChat.ta is the common textarea for appending the messages.
It's not clear to me from the pieces of your code that I have seen how
this actually is supposed to function. But that is really irrelevant,
what we need here is an SSCCE (see http://sscce.org).
What I have seen of your code though does have some problems. It would
be good if you could provide the version of Java that you are using and whether the text area you are writing to is a JTextArea, an AWT TextArea
or a JavaFX TextArea, all have different requirements for threading issues.
just one time when i have created the chat class. here lemme show. here is the full code .
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;
class mainServer
{
public static void main(String args[])
{
if(args.length!=1)
{
System.out.println("Port Number Required");
System.exit(-1);
}
try(ServerSocket server = new ServerSocket(Integer.parseInt(args[0])))
{
while(true)
new chatServerThread(server.accept());
}catch(Exception e){e.printStackTrace();}
}
}
class chatServerThread implements Runnable
{
Socket client;
Thread t;
String clientName,s="";
chatServerThread(Socket socket)
{
client=socket;
t=new Thread(this,"newThread");
t.start();
}
public void run()
{
try
{
DataInputStream in = new DataInputStream(client.getInputStream());
s="Joined";
clientName=in.readUTF();
clientChat.ta.append(clientName+" : "+s);
new Thread(new Runnable(){
public void run()
{
while(!s.equals("terminate"))
{
try
{
s=in.readUTF();
clientChat.ta.append(clientName+" : "+s);
}catch(Exception e){e.printStackTrace();}
}
}
}).start();
}catch(Exception e){e.printStackTrace();}
}
}
class clientChat
{
public JFrame f1 = new JFrame();
public JLabel name = new JLabel("Enter Name : ");
public JTextField namet = new JTextField(20);
public JLabel port = new JLabel("Enter Port Number : ");
public JTextField portt = new JTextField(20);
public JLabel ip = new JLabel("Enter Server's IP : ");
public JTextField ipt = new JTextField(20);
public JButton connect = new JButton("Connect To Server");
public JLabel message = new JLabel("Write Message : ");
public JTextField messaget = new JTextField(20);
public static JTextArea ta = new JTextArea();
public static JScrollPane jsp = new JScrollPane(ta);
public JButton send = new JButton("Send Message");
public JButton terminate = new JButton("Close Session");
Socket client=null;
DataOutputStream out=null;
clientChat()
{
f1.setSize(525,400);
send.setEnabled(false);
ta.setEditable(false);
f1.setResizable(false);
messaget.setEditable(false);
f1.setTitle("Chat Window");
f1.setLocationRelativeTo(null);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout();
f1.setVisible(true);
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
try
{
out.writeUTF(messaget.getText());
}catch(Exception e){e.printStackTrace();}
}
});
terminate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
try
{
out.writeUTF("terminate");
send.setEnabled(false);
connect.setEnabled(true);
namet.setEditable(true);
ipt.setEditable(true);
portt.setEditable(true);
messaget.setEditable(false);
client.close();
}catch(Exception e){e.printStackTrace();}
}
});
connect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
if(namet.getText().length() == 0 || portt.getText().length() == 0 || ipt.getText().length() == 0)
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(f1,"Make Sure These Details Are Filled: \n1:Name\n2:Port Number Of Server\n3:Server's IP Address","Notice",JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
client = new Socket(ipt.getText(),Integer.parseInt(portt.getText()));
out = new DataOutputStream(client.getOutputStream());
out.writeUTF(namet.getText());
connect.setEnabled(false);
namet.setEditable(false);
messaget.setEditable(true);
ipt.setEditable(false);
portt.setEditable(false);
send.setEnabled(true);
}catch(Exception e){e.printStackTrace();}
}
}
});
}
public void setLayout()
{
GroupLayout gl = new GroupLayout(f1.getContentPane());
f1.getContentPane().setLayout(gl);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(
gl.createParallelGroup()
.addGroup(gl.createSequentialGroup()
.addComponent(name)
.addComponent(namet)
.addComponent(connect))
.addGroup(gl.createSequentialGroup()
.addComponent(port)
.addComponent(portt))
.addGroup(gl.createSequentialGroup()
.addComponent(ip)
.addComponent(ipt))
.addGroup(gl.createSequentialGroup()
.addComponent(message)
.addComponent(messaget)
.addComponent(send))
.addComponent(jsp)
.addComponent(terminate)
);
gl.setVerticalGroup(
gl.createSequentialGroup()
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(name)
.addComponent(namet)
.addComponent(connect))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(port)
.addComponent(portt))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ip)
.addComponent(ipt))
.addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(message)
.addComponent(messaget)
.addComponent(send))
.addComponent(jsp)
.addComponent(terminate)
);
gl.linkSize(SwingConstants.HORIZONTAL,name,port,ip,message);
gl.linkSize(SwingConstants.HORIZONTAL,namet,portt,ipt,messaget);
gl.linkSize(SwingConstants.HORIZONTAL,connect,send);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new clientChat();
}
});
}
}
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 74:33:06 |
Calls: | 6,657 |
Calls today: | 3 |
Files: | 12,203 |
Messages: | 5,332,523 |
Posted today: | 1 |