I am making chat app with publisher and subscriber
I got two class one for the chat frame and one is chat member’s class.
The member class get the message from jtextfield and the chat member publisher send it back to jtextarea.
I get difficult to get back the text to the jtextarea from the subscriber MessageListener
@Override
public void onMessage(Message m)
{
try
{
TextMessage textMessage = (TextMessage) m;
System.out.println("Received:" + textMessage.getText());
}
catch (Exception e)
{
e.printStackTrace();
}
}
MessageListener listener = new MessageListener()
{
@Override
public void onMessage(Message m)
{
try
{
TextMessage textMessage = (TextMessage) m;
System.out.println("Received:" + textMessage.getText());
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
private void sendChatBattonActionPerformed(java.awt.event.ActionEvent evt) {
String empty="";
String message= inputChatText.getText();
if (message.equals(empty)){
inputChatText.setText("");
inputChatText.requestFocus();
} else {
inputChatText.setText("");
try {
String newMessage=userName+"~"+message+"~chat";
sendMessage(newMessage);
} catch (JMSException ex) {
Logger.getLogger(chatFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Thanks.
With the code snippet provided this is what I understand, To append text in the text area use
append.Say your class which acts as the Chat Frame is called
ChatFrameand has aMessageListener, your code in that case will look like this:2nd Approach where your
ChatFrameandMessageListenerare in two separate Java Source file, your code can look like below: