类似问题有一些例子 (Progress Bar Java)和(Java uploading percentage progressbar thread),但不了解如何使其适用于我的代码。
我的主要有2个面板一个在另一个上面(其中一个是带有2个标签的tabbedPane)
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();
在这个对象中,我有一个JButton convertArchiveButton
,我从Controller
类
public void addConvertArchiveButton(ActionListener ConvertArchiveButton) {
convertArchiveButton.addActionListener(ConvertArchiveButton);
}
这里是Controller中Listener的实现
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
}
我的示例尝试在Runnable
和EventQueue
添加一些新的SwingUtilities
,但不起作用。 SwingWorker
课程会有用吗?不太了解其用途。
基本上,int progress
会传递给JProgressBar
中的SMRPanel
。
使用ControlAltDel编辑回答:
仍然无法正常工作,converter.readFile(file);
无法接听。
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);
}
}
答案 0 :(得分:3)
您的问题是您在EDT线程中运行循环。通过在另一个线程中运行此操作进行修复,然后在完成文件处理时在EDT中设置更新的进度值
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();
}
}