java - Swing worker 不工作

标签 java swingworker

在我的应用程序中,我有两个不同的 Swing worker (他们从事两项不同的工作)。第一个完美地工作,而后者则不然。

我可以做什么来解决这个问题?

class worker extends SwingWorker<Boolean, String> {     
    @Override
     protected Boolean doInBackground() throws Exception {
        getstub().registration(name.getText(), surname.getText());
      return true;
     }

     protected void done() {
      boolean status;
      try {
       status = get();
       regstatus.setText("registration status: " + status);
      } catch (InterruptedException e) {
      } catch (ExecutionException e) {
      }
     }
}


class worker1 extends SwingWorker<Boolean, String> {        
    @Override
     protected Boolean doInBackground() throws Exception {
        int i = Integer.parseInt(id.getText());
        double r = Double.parseDouble(range.getText());
        double la = Double.parseDouble(lat.getText());
        Coordinate lat = new DegreeCoordinate(la);
        double lo = Double.parseDouble(lon.getText());
        Coordinate lon = new DegreeCoordinate(lo);
        System.out.println("id: "+i+" lat: "+la+ " lon: "+lo+" type: "+type.getText()+ " range: "+r);
        getstub().request(i, lat ,lon ,type.getText(), r);          
        return true;
     }

     protected void done() {

      boolean status;
      try {
       status = get();
       reqstatus.setText("request status: " + status);
      } catch (InterruptedException e) {
      } catch (ExecutionException e) {
      }
     }
}

和两个启动worker的jbutton

 private JButton register = new JButton("Register");
 register.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
              new worker().execute();
           }
          });

private JButton search = new JButton("Search now");
search.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                   reqstatus.setText("I'm searching...");
                   new worker1().execute();                    
               }
              });

最佳答案

大范围的问题

看起来您正在使用 ninja-exception-logging(不可见的):

  } catch (InterruptedException e) {
  } catch (ExecutionException e) {
  }

在那里进行一些实际的日志记录,您很可能会发现问题。至少使用 System.out.println();更好的选择是 log4j .

  } catch (InterruptedException e) {
      //a thread exception, quite unlikely to happen
      System.out.println(e);
  } catch (ExecutionException e) {
      System.out.println(e);
  }

特定错误

启用日志记录后,发现包装在 ExecutionException 中的 NumberFormatException

关于java - Swing worker 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832173/

相关文章:

java - 重分类异常(exception)是好的做法吗?

java - 无法覆盖 SwingWorker 中的 process() 方法

java - Swingworker 实例未同时运行

java - 等待 SwingWorker 完成

java堆大小增加并且内存不足

java - 避免没有集合的重复?

java - 如何使用 JDBC 查询 UTF-8 数据库

java - Swing Java 中的累积可运行项

java - 使用 SwingWorker 的带有 GUI 的倒计时器 - 未正确更新

java - 如何线程安全地读取和写入数据库?