简体   繁体   English

从另一个类追加到JTextArea

[英]Appending to JTextArea from another Class

my Reader class is used to display server messages whenever the server sends one. 我的Reader类用于在服务器发送消息时显示服务器消息。 This code works perfectly when printing directly to the command line, I receive ever message sent by the server. 当直接打印到命令行时,此代码非常有效,我收到服务器发送的消息。 However, the JTextArea only shows every other line or so. 但是,JTextArea仅显示每隔一行左右的内容。 I am not sure what to do here. 我不确定在这里做什么。 I read something about InvokeLater, but I am unsure if this is the correct situation to use it. 我读了一些有关InvokeLater的内容,但不确定是否适合使用它。

Also, the Thread is started from a GUI class when I click the connect button. 另外,当我单击连接按钮时,线程是从GUI类启动的。

public class Reader implements Runnable {

    public static Socket s = null;
    public static JTextArea TextArea;
    public static BufferedReader reader = null;

    /*Takes in a Socket. Sets up a input stream.
     * 
     */
    public Reader(Socket sIn, JTextArea display) {
        TextArea=display;
        s = sIn;
        try {
            reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
        } catch (IOException ex) {
            System.out.println("Error in constructor Reader: " + ex);
        }
    }

    public void checker() {
        try {
            /*Sits and waits for a Server Response*/
            while (!reader.ready()) {}

            System.out.println(reader.readLine());
            TextArea.append(reader.readLine()+"\n");

        } catch (Exception ex) {
            System.out.println("checker is crying: " + ex);
        }
    }


    public void run() {
        while (true) {
            checker();
        }
    }
}

Thank you for your replies and examples. 感谢您的答复和示例。 I have added the following to the checker() method : 我已经将以下内容添加到checker()方法:

EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            TextArea.append(reader.readLine() + "\n");
        } catch (Exception eee) {
            System.out.println("Error" + eee);
        }
    }
});

The same problem exists (receives only every other server message) but now it locks up on every other message. 存在相同的问题(仅接收其他所有服务器消息),但是现在它锁定其他所有消息。 If my server send 4 messages, it skips the first one gets the second etc.. Thank you for your help! 如果我的服务器发送4条消息,它将跳过第一个消息,而跳过第二个消息,等等。谢谢您的帮助!

You're reading from the BufferedReader twice: 您从BufferedReader中读取了两次:

        System.out.println(reader.readLine()); // *** here ***
        TextArea.append(reader.readLine()+"\n");  // *** and here ***

So that's why you're only seeing every other line. 这就是为什么您只看到其他所有行的原因。 Your program likely freezes due to your trying to call readLine() on the event thread -- don't do that. 由于您试图在事件线程上调用readLine() ,因此程序可能会冻结-请勿这样做。

Only read from the BufferedReader once for each line. 每行只能从BufferedReader中读取一次。 I'd also change the while loop, and would do the buffered reader's read outside of the Runnable. 我还将更改while循环,并在Runnable 之外进行缓冲读取器的读取。

   private String line = ""; // a class field

   public void checker() {
      try {

         while ((line = reader.readLine()) != null) {

            System.out.println(line);
            SwingUtilities.invokeLater(new Runnable() {
               TextArea.append(line + "\n");
            });
         }

      } catch (Exception ex) {
         System.out.println("checker is crying: " + ex);
      }
   }


   public void run() {
      checker();
   }

Or better -- use a SwingWorker object and publish/process the line to your JTextArea. 或更好-使用SwingWorker对象并将行发布/处理到JTextArea。

As an aside, you'll want to learn about and have your code comply with Java naming conventions including having all class names start with an upper case letter and all variables and methods begin with a lower case letter. 顺便说一句,您将要学习并使代码符合Java命名约定,包括使所有类名以大写字母开头,而所有变量和方法都以小写字母开头。 Doing this will allow others to more easily and quickly understand your code, and will likely help you get quicker and better help here. 这样做将使其他人可以更轻松,快速地理解您的代码,并且可能会帮助您在此获得更快更好的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM