1

I have created a server thread class in netbeans and used the netbeans swing auto generated Jframe to created a GUI application that invokes the class. I want to append a string value from the thread class to my JtextArea, but the value displayed on my Jtextarea is null .The string is not returned Please help me. The code sample is as follows

public class simpletestserver1 extends Thread {
String message,mess;
public void run(){
.
.//some coding here
.
.
DataOutputStream outToClient = new DataOutputStream(c.getOutputStream()); 
Scanner r = new Scanner(c.getInputStream());
outToClient.writeBytes(m+'\n');

mess=r.nextLine(); 
// THIS IS THE MESSAGE THAT NEEDS TO BE APPENDED TO 
// MY JTEXTAREA IN MY JFRAME CLASS 

Now I have another client thread that send data to the server. The server thread has already started to listen when the program runs on another action event. Now,nn a button press action, my client thread start sending data to my server and the text should be appended to my JtextArea the jframe class is as follows:

package sdiappgui;
import java.util.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import java.awt.Window;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level; 
import java.util.logging.Logger;
public class SendEmail extends javax.swing.JFrame {
public SendEmail() {
initComponents();
}
. //some coding here for other generated components
.at this point my server thread has already started on a previously clicked button    action and it is already listening ,i start my client thread and the data sent to server should be appended to my jtextarea
.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)     {                                         
// TODO add your handling code here: 
final simpletestserver1 th1=new simpletestserver1();  
final simpletestclient1 th2=new simpletestclient2();                    
javax.swing.SwingUtilities.invokeLater(new Runnable() {
              @Override
        public void run() {
            th2.start();
            jTextArea2.append("received: " +th1.mess +'\n');     
        }
    });
}

However my jtextarea is not receiving any string returned by the client. A null is displayed on the jtextArea when i run the program. Please help me out.

1 Answers1

1

There is no code that would check if the client thread has already received the string. As you take the field value as soon as you start the client thread, it is much more likely that it has not done yet, and the initial value null is taken instead.

Move SwingUtilities.invokeLater call into the run method of the thread th1 after the line where the variable mess is assigned. Remove th1.start() from there.

// Inside th2.run method:
mess=r.nextLine(); 
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        jTextArea2.append("received: " +th2.mess +'\n');     
    }
 });
}


public SendEmail() {
  initComponents();
  th2.start();
}
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • my thread class is not included in the Jfram class and I cannot invoke it as it is a private class,my server has already started listening previously, the th1.start() here is my client thread.please see my updated description. – user2013624 Feb 01 '13 at 15:26
  • Sorry, overlooked. Surely this is th2, not th1. But which value then do you expect to get from the th1.mess reference? Looks like a wrong object to take a value from. – Audrius Meškauskas Feb 01 '13 at 15:39
  • Actually i want to receive the message that the client has given to the server. The server reads the message and outputs it on my jtextarea. My server has been started initially on another key press and t is already listening for message from client.thats why i have referenced the th1.mess, it's expected to read message from client and output the msg on my jtextarea! – user2013624 Feb 01 '13 at 19:17