Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Syncronising two classes  RSS feed

 
gowtham potter
Greenhorn
Posts: 27
Firefox Browser MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i am writing an im client in java using Smack API.
But I have a problem,
The message is recieved and sent by another class and i want it communicating with my main GUI class.
this is the scenario
How can i get it to communicate with my main class.
the main class uses an object of the ChatRecieverandSender class,so if i get a message ,i want it to be sent to the main class this is how i recieve a a message.
Chat newChat = chatmanager.createChat("jsmith@jivesoftware.com", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});

I want message to be sent to the main class and it to be notified when a msg is recieved help plezzz
 
Mohamed Sanaulla
Bartender
Posts: 3174
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am not sure if I have understood your requirements correctly- Basically you need to be Asynchronously receiving and sending the IM messages. And these IM messages must be displayed on the GUI.

You would be using Socket programming here. So you need to have 2 Threads- One for reading the messages from the Socket input stream, one for sending the messages (writing to the Socket output stream) and one more thread for displaying the GUI. Usually GUI is managed in a separate thread. So you can designate one GUI component- (may be JTextArea) for displaying the received messages and another component (may be JTextField/JTextArea) for typing the messages. So when ever the Reader Thread detects some data in Socket input stream- You can use Scanner(or anyother related API) to read the String from the Input stream and then append the text in the JTextArea designated for displaying the chat messages. This can happen in the run() of your Thread/Runnable which you are using to construct your Reader Thread.

For writing- You can type in the text message, and the Writer Thread can pick this text and then send it to the Socket output stream. Another way is that- Your main thread can take care of sending of the chat messages. So when ever the user types the message and clicks on Send, the Eventhandler for the button can take the data from the Field and send it to the Socket output stream.

If you are having this- Head First java book- It has an sample Chat client implemented. You could refer that as well. I think you can download the code from the Head First java website- http://headfirstlabs.com/books/hfjava/
 
gowtham potter
Greenhorn
Posts: 27
Firefox Browser MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i must create two threads and send messages between them.
Should I implement Runnable or extend Thread.
P.S:-Hoe do I sent String from one to another thread?
Thanks saw your blog,it was nice
 
Mohamed Sanaulla
Bartender
Posts: 3174
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gowtham sundaar wrote:So i must create two threads and send messages between them.
Should I implement Runnable or extend Thread.
P.S:-Hoe do I sent String from one to another thread?
Thanks saw your blog,it was nice


There's no need to send messages between them. For the Reader Thread- Which reads for incoming data on the Socket, you just have to read the data and then display it in the GUI component.

For sending the chat messages- You can as well use the GUI thread to do that (I believe that the GUI will be launched as a separate thread using SwingUtilities.invokeLater).
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your Smack API is handling the Socket communication for you. This hopefully means that it is handling the Threading as well. Since it provides listeners for communicating the message, you do not have to worry about such things. All you need to do is provide a way of consuming that message - and the best place to get started is with a method. For example, to modify their example code to use a method in the Main class to process the message:


There may be other methods, but I didn't ready the API. The key here is that your MainChat class would know about the GUI, and would know about the Chat, and would be able to manage sending messages between the two.
 
gowtham potter
Greenhorn
Posts: 27
Firefox Browser MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i get it thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!