简体   繁体   English

我的代码无法正常工作,这是怎么回事? SwingWorker的

[英]my code does not work properly what is wrong with it? SwingWorker

I have an issue with my swing project , and after long time, I found that I have a concurrency issue. 我的swing项目有问题,很长一段时间后,我发现我有一个并发问题。 Nevertheless, I am clueless about concurrency issues ,and I just try to learn. 尽管如此,我对并发问题一无所知,我只是尝试学习。 My project in nutshell is a database that gets connected to a JTable . 简而言之,我的项目是连接到JTable的数据库。 When some operations such as enter, update, and delete happens, it affects both database which is derby and JTable at the same time. 当发生诸如输入,更新和删除之类的某些操作时,它将同时影响derby和JTable这两个数据库。 I use SwingWorker and invokeLater in my code. 我在代码中使用了SwingWorkerinvokeLater Sometimes it works, but some other times it doesnot work. 有时它可以工作,但另一些时候它却无法工作。 my code is as follows Note: I implement AbstractTableModel for my JTable which I advice I should have used DefualtTableModel . 我的代码如下:注意:我为JTable实现AbstractTableModel ,我建议我应该使用DefualtTableModel the following code is for deleting part and I think I can apply the same functionality for other operations. 以下代码用于删除部分,我想我可以将相同的功能应用于其他操作。 am I right about it? 我对吗?

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

        deleteDB = new DeleteDB();
        deleteDB.execute();

}                                        
class DeleteDB extends SwingWorker<Void, Void> {

    @Override
    public Void doInBackground() {
        try {
            ed.deleteQuery(name1);
        } catch (SQLException ex) {
            Logger.getLogger(MyFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("deleteing isuse is " + ex);
        }
        return null;
    }
    @Override
    public void done() {
         if (jTable1.getSelectedRow() >= 0) {
        tm.removeRow(jTable1.getSelectedRow());
          } else {
        System.out.println("nothing is selected");
    }
    name.setText("");
    tel.setText("");
    ed.printData();
        JOptionPane.showMessageDialog(null, "Task completed");
    }

}

Please be kind and help to find out where my problem is. 请客气并帮助找出我的问题所在。 Please inform me if you need more information from me. 如果您需要我的更多信息,请通知我。

Your basic code in the ActionListener is overly complicated. 您在ActionListener中的基本代码过于复杂。

  1. Code invoked within a listener executes on the Event Dispatch Thread, so there is no need for the SwingUtilities.invokeLater() 在侦听器中调用的代码在事件调度线程上执行,因此不需要SwingUtilities.invokeLater()
  2. for the same reason you don't need the synchronize block. 出于同样的原因,您不需要同步块。

I would place all the code in the SwingWorker. 我将所有代码放在SwingWorker中。 That is, first you should delete the data from the database (since this is the functionality that is most likely to fail). 也就是说,首先您应该从数据库中删除数据(因为这是最有可能失败的功能)。 If the deletete completes successfully then I would remove the row from the TableModel. 如果删除成功完成,那么我将从TableModel中删除该行。 So you could need to "publish" the row that you want to be deleted within the doInBackground() method. 因此,您可能需要在doInBackground()方法中“发布”要删除的行。 Then the code that executes in the process() method of the SwingWorker will automatically run on the EDT. 然后,在SwingWorker的process()方法中执行的代码将自动在EDT上运行。

See Tasks That Have Intermediate Results for more information. 有关更多信息,请参见具有中间结果的任务

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

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