【问题标题】:JProgress Bar Indeterminate mode not updatingJProgress Bar Indeterminate 模式未更新
【发布时间】:2013-08-06 18:56:20
【问题描述】:

我有一个 JNI 函数可能需要一段时间才能完成,我希望在完成该函数时运行一个处于不确定模式的 JProgress 栏。我已经阅读了 Oracle 提供的教程,但他们教程的性质似乎并不能帮助我理解如何去做。我意识到我应该在后台线程中运行这个函数,但我不太确定该怎么做。

这里是相关代码。我有一个按钮(runButton),按下时会调用函数 mainCpp():

public class Foo extends javax.swing.JFrame 
                         implements ActionListener,
                                    PropertyChangeListener{

    @Override
    public void actionPerformed(ActionEvent ae){
        //Don't know what goes here, I don't think it is necessary though because I do not intend to use a determinate progress bar
    }

    @Override
    public void propertyChange(PropertyChangeEvent pce){
        //I don't intend on using an determinate progress bar, so I also do not think this is necassary
    }

class Task extends SwingWorker<Void, Void>{

    @Override
    public Void doInBackground{
         Foo t = new Foo();
         t.mainCpp();

         System.out.println("Done...");
    }
    return null;
}

/*JNI Function Declaration*/
public native int mainCpp(); //The original function takes arguments, but I have ommitted them for simplicity. If they are part of the problem, I can put them back in.

...//some codes about GUI

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

    ProgressBar.setIndeterminate(true);
    Task task = new Task();
    task.execute();    
    ProgressBar.setIndeterminate(false);

}


/*Declarations*/
private javax.swing.JButton runButton;
}

任何帮助将不胜感激。

编辑:编辑以尝试执行 kiheru 建议的操作,但仍然无法正常工作。

【问题讨论】:

  • 您关于在 doInBackround() 中运行它的假设是正确的。 SwindWorkers 只需要一个execute() 调用即可开始运行,因此如果您提前准备好worker,您可以在按钮操作中调用它。或者,您可以就地创建一个 SwingWorker,然后执行它。
  • 你能详细说明一下“提前准备工人”是什么意思吗?

标签: swing concurrency jprogressbar


【解决方案1】:

假设你有一个像这样的 SwingWorker:

class Task extends SwingWorker<Void, Void>{
    @Override
    public Void doInBackground() {
        // I'm not sure of the code snippets if you are already in a
        // Foo instance; if this is internal to Foo then you obviously do
        // not need to create another instance, but just call mainCpp().
        Foo t = new Foo();
        t.mainCpp();
        return null;
    }

    @Override
    public void done()
        // Stop progress bar, etc
        ...
    }
}

您可以在包含对象的字段中保留一个实例,然后使用它会像这样工作:

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // Start progress bar, disable the button, etc.
    ...
    // Task task has been created earlier, maybe in the constructor 
    task.execute();
}

,或者你可以就地创建一个worker:

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // Start progress bar, disable the button, etc.
    ...
    new Task().execute();
}

【讨论】:

  • 我相信这是我尝试过的(检查我的编辑),但进度条没有移动。
  • @SeanSenWang 不要在 runButtonActionPerformed() 中停止进度条 - 这将立即完成。而是在 SwingWorker 的done() 中执行此操作。还要注意关于new Foo() 的评论(不应该破坏任何东西,但可能不需要)
  • 啊,我需要在动作事件之外设置indeterminate(false)。非常感谢您!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多