简体   繁体   English

在SwingWorker中停止JProgressBar的不确定模式

[英]Stopping the indeterminate mode of of JProgressBar in SwingWorker

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). 一旦我的doInBackground (SwingWorker的方法)返回null (意味着我的任务完成时),我想停止进度条的不确定模式。 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
#

You appear to be using your SwingWorker wrong. 您似乎使用了错误的SwingWorker。 You should never call doInBackground() directly, and especially not right in the event dispatch thread -- this goes against the whole reason for using a SwingWorker -- but rather call execute on the SW. 你永远不应该直接调用doInBackground() ,特别是在事件调度线程中没有 - 这违背了使用SwingWorker的全部原因 - 而是在SW上调用execute。 Add a PropertyChangeListener to the SwingWorker and change behavior based on that. 将PropertyChangeListener添加到SwingWorker并根据该行为更改行为。

eg, 例如,

  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();

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

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