计算时 Java Swing ProgressBar 更新 [英] Java Swing ProgressBar update while computing
问题描述
有一些类似问题的例子(进度条 Java) 和 (Java 上传百分比进度条线程)但不明白如何使它适用于我的代码.
There are some example for similar question (Progress Bar Java) and (Java uploading percentage progressbar thread) but didn't understand how to make it work for my code.
我的主面板有 2 个面板,一个在另一个面板上(其中一个是带有 2 个选项卡的 tabbedPane)
My main has 2 panels one over the other (one of which is a tabbedPane with 2 tabs)
public class MainIRH {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
MainWindow window = new MainWindow();
JPanel mainPanel = new JPanel(new BorderLayout(0, 20));
mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
ControlPanel controlPanel = new ControlPanel();
tabbedPane.setBorder(new EmptyBorder(10, 10, 10, 10));
controlPanel.setBorder(new EmptyBorder(0, 20, 20, 20));
ECPanel ecPanel = new ECPanel();
SMRPanel smrPanel = new SMRPanel();
tabbedPane.addTab("Environnement Canada", null, ecPanel, "Convertir des données des stations d\'Environnement Canada");
tabbedPane.addTab("Station Météo Routière", null, smrPanel, "Convertir des données des stations météos routières");
mainPanel.add(tabbedPane, BorderLayout.NORTH);
mainPanel.add(controlPanel, BorderLayout.CENTER);
window.add(mainPanel);
new ControllerIRH(ecPanel, smrPanel, controlPanel);
window.setVisible(true);
}
});
}
}
重要的一点是这个对象:SMRPanel smrPanel = new SMRPanel();
The important bit is this object : SMRPanel smrPanel = new SMRPanel();
在这个对象中,我有一个 JButton convertArchiveButton
,我从 Controller
类中添加了一个 ActionListener
Inside this object I have a JButton convertArchiveButton
that I add a ActionListener from a Controller
class
public void addConvertArchiveButton(ActionListener ConvertArchiveButton) {
convertArchiveButton.addActionListener(ConvertArchiveButton);
}
这里是Controller中Listener的实现
Here's the the implementation of the Listener in the Controller
private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
converter.convert();
}
}
converter
是执行冗长操作的类
File[] listOfFiles = folder.listFiles();
for (int file = 0; file < listOfFiles.length; file++) {
int progress = Math.round(file * 100 / listOfFiles.length);
//tried a lot of things here...
//...computing
}
我的例子试图用 EventQueue
和 SwingUtilities
添加一些新的 Runnable
但没有工作.SwingWorker
类会有用吗?不太明白用法.
I example tried to add some new Runnable
with both EventQueue
and SwingUtilities
but didn't work. A SwingWorker
class would be useful? Don't quite understand the use.
基本上,int progress
将传递给 SMRPanel
中的 JProgressBar
.
Basically, int progress
would be passed to the JProgressBar
in the SMRPanel
.
使用 ControlAltDel 编辑答案:
EDIT WITH ControlAltDel answer :
仍然不起作用,converter.readFile(file);
没有被调用.
Still doesn't work, converter.readFile(file);
doesn't get called.
private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
File[] listOfFiles = folder.listFiles();
System.out.println(listOfFiles.length);
for (int i = 0; i < listOfFiles.length; i++) {
final File file = listOfFiles[i];
final int idx = (i * 100) / listOfFiles.length;
Thread t = new Thread(new Runnable() {
public void run() {
converter.readFile(file);
System.out.println(idx);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
smrPanel.setProgressBarValue(idx);
}
});
}
});
try {
t.start();
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
converter.convert(year);
}
}
推荐答案
您的问题是您在 EDT 线程中运行循环.通过在另一个线程中运行它来修复,然后在完成处理文件后在 EDT 中设置更新的进度值
Your problem is that you are running your loop in the EDT thread. Fix by running this in another thread, and then setting the updated progress value in the EDT when you finish processing a file
private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
new Thread(new Runnable() {
public void run() {
for (int file = 0; file < listOfFiles.length; file++) {
final File f = listOfFiles[i];
final idx = file;
Thread t = new Thread(new Runnable() {
converter.convert(f);
swingUtilities.invokeLater(new Runnable() {
myProgressBar.setValue(idx);
});
t.start();
t.join();
});
}
}).start();
}
}
这篇关于计算时 Java Swing ProgressBar 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!