将线程类的字符串值附加到jtextArea [英] Append a string value from thread class to jtextArea

查看:100
本文介绍了将线程类的字符串值附加到jtextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在netbeans中创建了服务器线程类,并使用netbeans swing自动生成的Jframe创建了调用该类的GUI应用程序.我想将线程类的字符串值附加到我的JtextArea上,但是我的Jtextarea上显示的值是null.未返回该字符串.请帮助我.代码示例如下

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 

现在,我有另一个将数据发送到服务器的客户端线程.当程序在另一个操作事件上运行时,服务器线程已经开始侦听.现在,按下按钮,我的客户端线程开始向我的服务器发送数据,并且该文本应附加到我的JtextArea上 jframe类如下:

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');     
        }
    });
}

但是我的jtextarea没有收到客户端返回的任何字符串.当我运行程序时,在jtextArea上显示一个空值.请帮帮我.

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.

推荐答案

没有代码可以检查客户端线程是否已经 接收了该字符串.在启动客户端线程后立即获取字段值时,很有可能尚未完成,而是取初始值null.

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.

SwingUtilities.invokeLater调用移至分配了变量mess的行之后的线程th1run方法中.从此处删除th1.start().

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();
}

这篇关于将线程类的字符串值附加到jtextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆