在while循环中更新我的textArea [英] Updating my textArea whilst in a while loop

查看:131
本文介绍了在while循环中更新我的textArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java来创建UDP点对点聊天程序。

I'm using Java to create a UDP peer to peer chat program.

没有这个while while循环我的textArea

Without this while loop my textArea

    while(true)
    {
        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
        try {
            serverSocket.receive(incoming);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        String incomingMessage = new String (incoming.getData());
        textArea.setText("Client: " + incomingMessage);
        System.out.println(incomingMessage);
        textArea.setText("Client: " + incomingMessage);
    }

使用while循环我的textArea doe不更新,我尝试过使用在那里调用:

With the while loop my textArea doe not update, I've tried using an invoke in there:

                    java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
                    try {
                          textArea.append(msg);

但它似乎没有仍然工作。

But it didn't seem to work still.

我正在使用Eclipse Luna窗口核心构建器并自动生成此自动代码

I'm using Eclipse Luna window core builder and it automatically made this automated code

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Server window = new Server();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

这是一个自动化代码是wr itten,通过查看API,这会根据事件频繁更新GUI。

This is a the automated code that was written, and by looking at the API, this updates the GUI every so often depending on events.

然而,由于没有事件,它无法在while循环中更新。

However, it can't update in the while loop since there is no event.

我怎么能解决这个问题,谢谢。

How can I fix this, thank you.

推荐答案

您需要尊重Swing线程规则,这些规则总结得过于简单,意味着所有正在运行的任务,例如等待和捕获您的任务数据应该在后台线程中完成,所有Swing调用都应该在Swing事件线程上完成。使用 SwingWorker< Void,String> 来帮助您完成此操作。

You need to respect Swing threading rules which to summarize too briefly means that all running tasks, such as waiting for and capturing your data, should be done in a background thread, and all Swing calls should be done on the Swing event thread. Use a SwingWorker<Void, String> to help you do this.

请查看 Swing中的并发,以查看包含所有血腥细节的教程。

Please check out Concurrency in Swing to see a tutorial with all the gory details.

例如,

private class MySwingWorker extends SwingWorker<Void, String> {
  private byte[] buffer = new byte[2000];

  @Override
  protected Void doInBackground() throws Exception {
     while (true) {
        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
        serverSocket.receive(incoming);

        String incomingMessage = new String(incoming.getData());
        publish(incomingMessage);
     }
     return null;
  }

  @Override
  protected void process(List<String> chunks) {
     for (String chunk : chunks) {
        textArea.append("Client: " + chunk + "\n");
     }
  }
}

其他建议:


  • 在SwingWorker上调用 execute()来运行它。

  • 我几乎总是在执行它之前向我的worker添加一个PropertyChangeListener。

  • 在这个监听器中,我注意newValue是SwingWorker.StateValue.DONE。

  • 当我遇到这个时,我在我的SwingWorker上调用 get(),即使它返回Void。这是为了帮助我捕获并响应SwingWorker在运行时可能发生的任何异常。

  • You call execute() on the SwingWorker to run it.
  • I almost always add a PropertyChangeListener to my worker before executing it.
  • In this listener I watch for the newValue to be SwingWorker.StateValue.DONE.
  • When I encounter this, I call get() on my SwingWorker, even if it returns Void. This is to help me trap and respond to any exceptions that might have occurred within the SwingWorker as it was running.

这篇关于在while循环中更新我的textArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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