简体   繁体   English

使用SwingWorker更新Java Gui

[英]Update Java Gui Using SwingWorker

Below is a SwingWorker instance that did not update the Java GUI as I had expected it to. 下面是一个SwingWorker实例,它没有像我期望的那样更新Java GUI。

The SwingWorker is implemented inside an action listener for a button. SwingWorker在按钮的动作侦听器内部实现。 The min and max which define the loop are both a "final int" which are local to the action listener. 定义循环的minmax都是“ final int”,它们对于动作侦听器而言是本地的。

The SwingWorker is supposed to on each iteration call the NQueens object's go() method which recursively finds all the solutions for n-queens. SwingWorker应该在每次迭代时调用NQueens对象的go()方法,该方法递归地找到n个皇后的所有解。 Then the NQueens object's toString() is called after which the String is published so that the process() method will update the jProgressBar and the jTextArea . 然后NQueens对象的toString()然后发布String,以便process()方法将更新jProgressBarjTextArea However nothing occurs when the button is pressed. 但是,按下按钮后什么也不会发生。 Is this how I should be implementing an intensive process coupled with GUI component updates? 这是我应该如何实施密集的过程以及GUI组件更新吗?

new SwingWorker <Void,String>()
{       
     private int totalPercent;
     @Override
     protected Void doInBackground()
     {
         int diff = max - min + 1;
         int percent = 100;
         if(diff != 0)
         {
              percent = 100/diff;
         }
         totalPercent = 0;
         NQueens queens = new NQueens(min);
         for(int j = min; j <= max; j++)
         {
              queens.go();
              totalPercent += percent;
              String newText = queens.toString();
              publish(newText);
              queens.nextSet();
         }

         isCalced = true;

         return null;
      }

      protected void process(String results)
      {
           jTextArea2.append(results);
           jProgressBar1.setValue(totalPercent);
           jProgressBar1.repaint();
      }                                 
  }.execute();  

Could the reason be that you've not implemented the right process method? 原因可能是您没有实施正确的处理方法吗? I believe the correct signature of process would be 我相信流程的正确签名是

process(List<String> results) {
  for(String result : results) {
    jTextArea2.append(result);
  }
}

With regards to updating the progressbar; 关于更新进度条; the doInBackground method runs in a separate thread while the process method runs on the thread you invoked the worker from. doInBackground方法在单独的线程中运行,而process方法在您从其调用工作程序的线程上运行。 You should not write to the totalPercent variable from doInBackground and read it from process(). 您不应从doInBackground写入totalPercent变量,而应从process()读取它。 See thread visibility for more on this. 有关更多信息,请参见线程可见性 Instead, create something like a class called ProgressUpdateEvent which can hold a percent value and a message. 而是创建一个类似名为ProgressUpdateEvent的类,该类可以包含一个百分比值和一条消息。 That way, you could do as follows : 这样,您可以执行以下操作:

new SwingWorker <Void,ProgressUpdateEvent>() {

  protected Void doInBackground() {
    for (int i = 0; i <= 100; i++) {
      publish(new ProgressUpdateEvent("At " + i + "% right now", i);
    }
  }

  protected void process(List<ProgressUpdateEvent> events) {
    for (ProgressUpdateEvent event : events) {
      jTextArea2.append(event.getMessage());
      jProgressBar1.setValue(event.getPercent());
    }
  }
}

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

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