3

I want to stop the indeterminate mode of the progress bar, once my doInBackground (method of SwingWorker) returns null (meaning when my task is done). Here is my code inside the button; when I run my code, I get an error. Here is the code:

private void StartButtonMouseClicked(java.awt.event.MouseEvent evt) {                                         

final Main f22 = new Main();

initializer();

f22.getfile(FileName, 0);
f22.execute();

SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        jProgressBar1.setIndeterminate(true);
        try {
            if (f22.doInBackground() == null) {
                jProgressBar1.setIndeterminate(false);                        
            }
        } catch (IOException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});

Here is the error that I get:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6e1b0750, pid=4988, tid=5464
#
# JRE version: 7.0-b141
# Java VM: Java HotSpot(TM) Client VM (21.0-b11 mixed mode, sharing windows-x86              
# Problematic frame:
# V  [jvm.dll+0xa0750]
#
# Failed to write core dump. Minidumps are not enabled by default on client  

  versions of Windows
#
4

1 回答 1

5

您似乎错误地使用了 SwingWorker。您永远不应该doInBackground()直接调用,尤其是在事件调度线程中不正确——这与使用 SwingWorker 的全部原因背道而驰——而是在 SW 上调用 execute。将 PropertyChangeListener 添加到 SwingWorker 并基于此更改行为。

例如,

  final Main f22 = new Main();
  initializer();
  f22.getfile(FileName, 0);
  f22.addPropertyChangeListener(new PropertyChangeListener() {
     @Override
     public void propertyChange(PropertyChangeEvent pcEvt) {
        if (pcEvt.getNewValue().equals(SwingWorker.StateValue.DONE)) {
           // do your stuff here
        }
     }
  });
  f22.execute();
于 2012-05-19T13:04:26.963 回答