Java 计算时 Swing ProgressBar 更新
Java Swing ProgressBar update while computing
有一些类似问题的例子
(Progress Bar Java) and (Java uploading percentage progressbar thread) 但不明白如何让它适用于我的代码。
我的主面板有 2 个面板(其中一个是带有 2 个选项卡的选项卡面板)
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
class
添加了一个 ActionListener
public void addConvertArchiveButton(ActionListener ConvertArchiveButton) {
convertArchiveButton.addActionListener(ConvertArchiveButton);
}
下面是Controller中Listener的实现
private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
converter.convert();
}
}
converter
是执行冗长操作的 class
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
class 会有用吗?不太明白用途
基本上,int progress
会传递给 SMRPanel
中的 JProgressBar
。
使用 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);
}
}
你的问题是你是 运行 你在 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();
}
}
有一些类似问题的例子 (Progress Bar Java) and (Java uploading percentage progressbar thread) 但不明白如何让它适用于我的代码。
我的主面板有 2 个面板(其中一个是带有 2 个选项卡的选项卡面板)
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
class
public void addConvertArchiveButton(ActionListener ConvertArchiveButton) {
convertArchiveButton.addActionListener(ConvertArchiveButton);
}
下面是Controller中Listener的实现
private class ConvertArchiveButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
converter.convert();
}
}
converter
是执行冗长操作的 class
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
class 会有用吗?不太明白用途
基本上,int progress
会传递给 SMRPanel
中的 JProgressBar
。
使用 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);
}
}
你的问题是你是 运行 你在 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();
}
}