Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:42:41+00:00 2026-06-04T14:42:41+00:00

I am making chat app with publisher and subscriber I got two class one

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:42:42+00:00Added an answer on June 4, 2026 at 2:42 pm

    With the code snippet provided this is what I understand, To append text in the text area use append.

    @Override
    public void onMessage(Message m) {
        try {
            TextMessage textMessage = (TextMessage) m;
            System.out.println("Received:" + textMessage.getText());
            jTextArea.apped(textMessage.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    Say your class which acts as the Chat Frame is called ChatFrame and has a MessageListener, your code in that case will look like this:

    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.TextMessage;
    import javax.swing.JTextArea;
    
    
    public class ChatFrame {
        JTextArea textArea = null;
    
        void init(){
            //create and show UI here
        }
    
        class MessageListener implements javax.jms.MessageListener {
            @Override
            public void onMessage(Message arg0) {
                TextMessage textMessage = (TextMessage)arg0;
                try {
                    textArea.append(textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    2nd Approach where your ChatFrame and MessageListener are in two separate Java Source file, your code can look like below:

    ChatFrame
    
    import javax.swing.JTextArea;
    
    public class ChatFrame {
        JTextArea textArea = null;
    
        void init(){
            //create and show UI here
    
            //Also start your message listener and give it the reference of textArea to be updated
            MessageListener messageListener = new MessageListener(this.textArea);
        }
    }
    
    MessageListener
    
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.TextMessage;
    import javax.swing.JTextArea;
    
    
    public class MessageListener implements javax.jms.MessageListener {
    
        private JTextArea textArea = null;
    
        public MessageListener(JTextArea textArea){
            this.textArea = textArea;
            createConnection();
        }
    
        private void createConnection(){
            //Create your JMS Connection and add yourself as subscriber
        }
    
        @Override
        public void onMessage(Message arg0) {
            TextMessage textMessage = (TextMessage)arg0;
            try {
                textArea.append(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a chat window there are two method one for sending message
I'm making a small chat application. I got to the point where one client
I'm making chat application and read messages data from SQLite database. I want to
I'm making a chat application that works with long-polling to emulate a push from
I am making a chat window, i just want that message and date time
I am making a TCP chat server from a simple tutorial I found and
I'm making a chat similar to the Facebook's or Gmail's one, but something is
I'm making simple php chat, and i need to refresh messages in one div.I
I'm interested in making an in-company encrypted peer-to-peer chat app in Cocoa, but I
In a portion of my app I need to make a chat between two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.