0

I'm doing a chat room with.I have one jFrame created with Netbeans but I have one problem. I have 2 important classes. One of them is the interface itself. Another one is a java class with run method that ckecks for new messages. I will start a thread that will run this run method. Every time I read a message, I want to use a method send of the interface. This method will append the message to JEditorPane. But it's not appending it. I do receive a message but it is not appearing in the jeditorpane... Could you help me, please?

This method is on ClientForm1( interface ). It will append a text to a editorpane. I start here a thread to check the incoming messages

public void send(String message){
    String messages;
    messages = jEditorPane1.getText();
    jEditorPane1.setText(messages+message+"\n");
}

And this is the run method for checking incoming messages from server. All the JFrame variables are private.

public ResMesClient(Socket socket, BufferedReader br, PrintStream p){
    this.socket = socket;
    is=br;
    ps = p;
}
@Override
@SuppressWarnings("empty-statement")
public void run(){
    String messages;
    try {
    //This thread will read the server messages
       ClientForm1 cf = new ClientForm1(socket,ps); //I create an object
       JFrame frame1 = cf;  //I create a frame and set it visible.
       frame1.setVisible(true);
    while(true){
        messages = is.readLine();
        System.out.println("From ResMesClient :  "+messages);
        if(!messages.equals("QUITCLIENT"));
        else cf.send("Set text works"); //I call a send method of ClientForm1 class
    }
   } catch (IOException ex) {
   }

These two are 2 different java classes in different files. But it is not appending... I tried a lot of things but it's not working... At least, I can tell for sure that I do receive a message, only it's not appending

Thank you in advance

Olga
  • 50
  • 6
  • 1
    For reference, a complete, working example is shown [here](http://stackoverflow.com/a/3245805/230513). – trashgod Dec 14 '13 at 00:34
  • I just saw your complete code and i think the problem is just the socket and serversocket concept. I can provide you a simple example until sunday if you want. Additionally have a look at Java Net API and Socket programming. – Diversity Dec 14 '13 at 01:40

1 Answers1

0

Take care of calling SWT or Swing method from threads which not belong to the original Swing or SWT Thread -> Other Process or ThreadGroup.

Using SWT it means calling the text.setText method must be executed using the Display object

display.asyncExec(new Runnable() {
    public void run() {
       cf.send(message)
    }
});

using swing means

SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        cf.send(message); 
     }
 });

So your looü should look like this

while(true){
    messages = is.readLine();
    System.out.println("From ResMesClient :  "+messages);
    if(!messages.equals("QUITCLIENT")) {
        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
           cf.send(message); 
        }
    } else {

       //Quit programm or whatever
  }
});
}

I think there should be your problem.

Diversity
  • 1,890
  • 13
  • 20
  • THANK YOU SO MUCH. I'm literally crying now. I spent like 2 nights trying to solve this problem! Thank you so much! If it's not difficult for you, could you explain me briefly what does the SwindUtilities.invokeLater(new Runnable())? Thank you – Olga Dec 14 '13 at 01:55
  • Ok the problem is. You have to different Threads. One thread which controls your Socket network application and on Swing Thread. If you want to influence any kind of Swing UI from outside the SwingThread, it must be enqueued into the Swing (AWT-EventQueue" in order to show it in the correct way. This is done using the invokeLater method. This method ensures that the passed thread is properly inserted into the AWT-EventQueue and executed in the correct way by the Swing API – Diversity Dec 14 '13 at 02:09