Java的Swing Threading [英] Java's Swing Threading

查看:112
本文介绍了Java的Swing Threading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,如果我启动另一个线程来执行某些操作,我需要 SwingUtilities.invokeAndWait SwingUtilities.invokeLater 在我说的帖子中更新GUI。如果我错了,请纠正我。

My understanding is that if I start up another thread to perform some actions, I would need to SwingUtilities.invokeAndWait or SwingUtilities.invokeLater to update the GUI while I'm in said thread. Please correct me if I'm wrong.

我想要完成的是相对简单的:当用户点击提交时,我想(在执行任何操作之前)禁用提交按钮,执行操作,并在操作结束时重新启用按钮。我执行操作的方法在返回结果时直接更新GUI(显示结果)。

What I'm trying to accomplish is relatively straightforward: when the user clicks submit, I want to (before performing any actions) disable the submit button, perform the action, and at the end of the action re-enable the button. My method to perform the action updates the GUI directly (displays results) when it gets the results back.

此操作基本上查询服务器并获得一些结果。

This action basically queries a server and gets some results back.

到目前为止我所拥有的是:

What I have so far is:

boolean isRunning = false;

synchronized handleButtonClick() {
  if ( isRunning == false ) {
    button.setEnabled( false );
    isRunning = true;
    doAction();
  }
}

doAction() {
  new Thread() {
    try {
      performAction(); // Concern A
    } catch ( ... ) {
      displayStackTrace( ... ); // Concern B
    } finally {
      SwingUtilities.invokeLater ( /* simple Runnable to enable button */ );
      isRunning = false;
    }
  }
}

对于我上述两个问题,我是否必须使用 SwingUtilities.invokeAndWait ,因为他们都将更新GUI?所有GUI更新都围绕更新 JTextPane 。我是否需要在我的线程中检查我是否在EDT上,如果是,我可以调用我的代码(无论是否更新GUI)而不使用 SwingUtilities.invokeAndWait

For both of my concerns above, do I would have to use SwingUtilities.invokeAndWait since they both will update the GUI? All GUI updates revolve around updating JTextPane. Do I need to in my thread check if I'm on EDT and if so I can call my code (regardless of whether it updates the GUI or not) and NOT use SwingUtilities.invokeAndWait?

编辑:这是我现在正在做的事情:

Here is what I am doing now:

handleButtonClick() {
  if ( isRunning == true )
     return;
  disable button;
  SwingWorker task = new MyTask();
  task.execute();
}

...inside MyTask
doInBackground() {
  return performAction();
}

done() {
  result = get();
  enable button;
  isRunning = false;
  interpret result (do most of the GUI updates here);
}

虽然 performAction()做了一些GUI更新,我把它们包括在内:

While performAction() does some GUI updates, I have wrapped those in:

if ( SwingUtil.isEDT() )
  doGUIupdate()
else
  SwingUtil.invokeLater( new Runnable() {
    run() {
      doGUIupdate();
    }
  } );

希望这是朝着正确方向迈出的一步,如果您认为有更好的处理方法,请发表评论我的情况。

Hopefully this is a step in the right direction, please comment if you believe there are better ways to handle my situation.

推荐答案

在我看来,你几乎不应该使用 invokeAndWait()。如果需要一段时间来锁定你的UI。

In my opinion you should almost never use invokeAndWait(). If something is going to take awhile that will lock your UI.

使用 SwingWorker 来做这种事情。请参阅使用Java SE 6中的SwingWorker提高应用程序性能

Use a SwingWorker for this kind of thing. Take a look at Improve Application Performance With SwingWorker in Java SE 6.

这篇关于Java的Swing Threading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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