java - java清单当前正在执行SwingWorker并停止它

  显示原文与译文双语对照的内容

我在SwingWorker下面


public class WorkerTrying extends SwingWorker<Void, Void> {



 Statement stmt;


 Connection con;


 ResultSet rs;


 String connectionUrl ="jdbc:sqlserver://192.168.131.10;" +"databaseName=SomeDB;" +"user=" +"SomeUser" +";" +"password=" +"SomeUserPassword" +";";



 public void closeConnection() throws SQLException{


 stmt.close();


 System.out.println("Closed!!!");


 }



 protected Void doInBackground() throws Exception {



 String query ="Select column1,column2 from Table1"


 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");



 con = DriverManager.getConnection(connectionUrl);


 stmt = con.createStatement();



 rs = stmt.executeQuery(query);



 while (rs.next()){


//Doing something...


 }



 return null;


 }



 public void done() { 



 } 


 }



你可以看到在这个worker中的循环中通过了一些DB连接和数据 fetchinbg 。 我的问题是数据库查询需要很长时间。 我想为SwingWorker中的关闭语句和连接添加停止按钮,并取消immediatelly本身。

我正在尝试停止按钮,但没有帮助;


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 


 WorkerTrying workerTrying = new WorkerTrying();


 workerTrying.cancel(true);


 } 



我还有 3 -4 SwingWorkers 。 所以我必须检测哪一个当前正在运行。

问候。

====更新 ====

像这样的新停止按钮


jButtonStop.addActionListener(new java.awt.event.ActionListener() {


 public void actionPerformed(java.awt.event.ActionEvent evt) {


 for(SwingWorker workerTrying : _workers){


 if(!workerTrying.isDone()){


 try {


 ((WorkerTrying)workerTrying).closeConnection();


 } catch (SQLException ex) {


 Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);


 }


 workerTrying.cancel(true);


 }


 }



 }


 }


});



====第二问题更新 ====

启动按钮动作监听器就像这样;


startButton.addActionListener(new java.awt.event.ActionListener() {


 public void actionPerformed(java.awt.event.ActionEvent evt) {


 if(jTextField1.getText().equals("")){


 final SwingWorker workerTrying = new WorkerTrying();


 _workers.add(workerTrying);


 workerTrying.execute();


 }


 else{


 final SwingWorker workerTrying2 = new WorkerTrying2();


 _workers.add(workerTrying2);


 workerTrying2.execute(); 


 }


 }


});



你可以看到SwingWorker可以执行的某些条件。

而不是像这样停止按钮;


stopButton.addActionListener(new java.awt.event.ActionListener() {


 public void actionPerformed(java.awt.event.ActionEvent evt) {


 for (SwingWorker workerTrying : _workers) {


 if(!workerTrying.isDone()){


 try {


 ((WorkerTrying)workerTrying).closeConnection();


 } catch (SQLException ex) {


 Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);


 }


 workerTrying.cancel(true);


 }


 }


 for (SwingWorker workerTrying2 : _workers) {


 if(!workerTrying2.isDone()){


 try {


 ((WorkerTrying2)workerTrying2).closeConnection();


 } catch (SQLException ex) {


 Logger.getLogger(MainGui.class.getName()).log(Level.SEVERE, null, ex);


 }


 workerTrying2.cancel(true);


 }


 }


 }


 });



如果第一个 SwingWorker ( workerTrying ) 执行并尝试停止,一切都会正常。 语句已经关闭,SwingWorker成功取消。

但是当第二个 SwingWorker ( workerTrying2 ) 执行并想要停止时,异常throwed除外;


Exception in thread"AWT-EventQueue-0" java.lang.ClassCastException: testproject.MainGui$WorkerTrying2 cannot be cast to testproject.MainGui$WorkerTrying



时间: 原作者:

我用 List 追踪所有员工。 了,你可以试试。


 public class SwingT {


 private List<SwingWorker> _workers = new ArrayList<>();


 public SwingT(){


 JFrame frame = new JFrame("Test Worker"); 


 frame.setSize(320, 160);


 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


 frame.setLayout(new FlowLayout());


 JButton btn = new JButton("Add Work");


 btn.addActionListener(new java.awt.event.ActionListener() {


 public void actionPerformed(java.awt.event.ActionEvent evt) {


 SwingWorker workerTrying = new WorkerTrying();


 _workers.add(workerTrying);


 workerTrying.execute();


 }


 });



 JButton btn2 = new JButton("Stop All");


 btn2.addActionListener(new java.awt.event.ActionListener() {


 public void actionPerformed(java.awt.event.ActionEvent evt) {


 for(SwingWorker workerTrying : _workers){


 if(!workerTrying.isDone()){


//Close DB Connections


 ((WorkerTrying)workerTrying).closeConnection();


 workerTrying.cancel(true);


 }


 }



 }


 });


 frame.add(btn);


 frame.add(btn2);


 frame.setVisible(true);


 }



/**


 * @param args


 */


 public static void main(String[] args) {


 new SwingT();



 }



}



更新


public class WorkerTrying extends SwingWorker<Void, Void> {



public void closeConnection(){


//Close the connection


 System.out.println("closed");


}



..........


}



...