如何异步触发 JProgressBar? [英] How to asynchronous trigger JProgressBar?
问题描述
我在 JPanel
中有一个 JButton
(swing
),如果按下它,我将在 for 循环上执行任务EDT 线程
中的列表.这样做时,我需要更新 JProgressBar
.
I have a JButton
(swing
) in the JPanel
, where if it is pressed I am performing a task on the for loop over the list in its EDT thread
. While doing so I need to update the JProgressBar
.
问题是,当我按下 JButton 时,任务是在事件调度线程 (EDT) 中执行的.所以我无法更新在主线程或 UI 线程中触发的 JProgressBar
.
Problem is, when I am pressing the JButton the task is performed in Event Dispatch Thread (EDT). So I can't update the JProgressBar
which is triggered either in main or UI thread.
现在我无法使用源代码,因为我完全改变了它并尝试在 JProgressBar
中使用 Eclipse SWT,当 Swing JButton
被触发时,它变得混乱.
Right now the source code is not available at me as I completely changed it and tried to use Eclipse SWT for JProgressBar
when that Swing JButton
is triggered, it becomes messy.
现在我遇到了invalid thread access
错误,因为 Display 对象在单独的 UI 线程中运行.一次,只显示 Swing JPanel
或 Eclipse SWT Shell
.
Now I got invalid thread access
error, as the Display object runs in separate UI thread. At a time, only either a Swing JPanel
or Eclipse SWT Shell
gets displayed.
我正在使用 JOptionPane
触发 JPanel
.
I am triggering JPanel
using JOptionPane
.
推荐答案
您需要在与 EDT
不同的线程中完成长时间运行的工作并更新进度条值.大多数情况下,SwingWorker
是一个很好的方法.因此,在该按钮的 ActionListener
中,您应该将长时间运行的线程与 EDT 分开.你可以使用裸 Thread
来完成,但是 Workers 和特别的 SwingWorker
是为这种情况设计的:
You need to do your long running job and updating the progress bar value in a separate Thread from EDT
. Most of the time SwingWorker
is a good way to do that. So in your ActionListener
of that button you should separate the long running thread from the EDT. You can do it with bare Thread
but Workers and specially SwingWorker
is designed for such cases:
package graphics;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class GraphicDev extends JFrame{
private static final long serialVersionUID = 679207429168581441L;
//
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 100;
private int percent;
private JProgressBar progress;
public GraphicDev() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(dim.width/2-FRAME_WIDTH/2, dim.height/2-FRAME_HEIGHT/2, FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
setLayout(new FlowLayout());
//
progress = new JProgressBar(0, 100);
progress.setStringPainted(true);
//
JButton actButton = new JButton("Start!");
actButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
for (long i = 0; i < 10000000000000L; i++) {
System.out.println(i);
if(i%200000 == 0){
progress.setValue(percent++);
}
if(percent >= 100){
break;
}
}
return null;
}
@Override
protected void done() {
JOptionPane.showMessageDialog(GraphicDev.this, "Done!");
}
}.execute();
}
});
//
add(actButton);
add(progress);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GraphicDev g = new GraphicDev();
g.setVisible(true);
}
});
}
}
希望这会有所帮助.
这篇关于如何异步触发 JProgressBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!