我有一个可能需要一段时间才能完成的 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 建议的事情,但仍然不起作用。