I am still having difficulty properly routing text messages when multiples users chat with one other person. I have a constructor that creates a new JFrame every time it’'s called, therefore creating a popup window and displaying the text received. Is it possible to create new objects (each object representing the chat window via JFrame) and it into a HashMap? I want to then retrieve the correct object via an index value which would be the user name sending the chat, and have the text be displayed in that JFrame.
I implemented it, but it’‘s not working. i don’'t know how to retrieve the object, and call JTextArea on it. Anyone work with this before? Perhaps give me an example that would work?
In case you’‘re wondering, this is what’'s currently happening:
Let’‘s say we have User2, and User3 who want to chat with User1. User2 and User3 send an initial message and it’‘s successfuly received, new chat windows are popped up. But now, whenever User2 or User3 type a message and send it to User1, it appears in User3’'s window! So messages are always received in the most recent chat object.
I’‘ve been struggling with this for some time, and haven’'t found a solution, so please help!
Perhaps, if you have a code snippet this would help. Conceptually, you shouldn’‘t have any issues, but I can’'t really figure out what is going wrong under the hood from your description.
You are using a Map currently then to store the references to the JFrames based on the userID?
You’'ll want to have the JID as the key in order to retrieve the chat based upon that key:
Hash.put(JID, chat);
/code
Chat in this case can be an object that stores a reference to your JFrame, or your JTextArea, whatever is most convienent for you. You could even add a method on this class to post a new message:
I thank you so much for your help. In case you were wondering, after many hours I finally discovered my problem: I declared my JTextArea as STATIC! GRRRRRRRR. Anyways, thanks so much for your help, I don’'t know how the reward system works on this forum, but if there is one, please let me know how I can compensate for your help!