Swing/ProgressMonitor の変更点
- 追加された行はこの色です。
- 削除された行はこの色です。
- Swing/ProgressMonitor へ行く。
- Swing/ProgressMonitor の差分を削除
---
category: swing
folder: ProgressMonitor
title: ProgressMonitorで処理の進捗を表示
tags: [ProgressMonitor, SwingWorker, PropertyChangeListener, JProgressBar]
author: aterai
pubdate: 2010-02-15T14:18:06+09:00
description: ProgressMonitorで処理の進捗を表示します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTRQXIAu4I/AAAAAAAAAg4/bh8niw_k5AE/s800/ProgressMonitor.png
---
* 概要 [#summary]
`ProgressMonitor`で処理の進捗を表示します。
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTRQXIAu4I/AAAAAAAAAg4/bh8niw_k5AE/s800/ProgressMonitor.png)
* サンプルコード [#sourcecode]
#code(link){{
ProgressMonitor monitor = new ProgressMonitor(this, "message", "note", 0, 100);
monitor.setProgress(0);
worker = new SwingWorker<String, String>() {
@Override public String doInBackground() {
int current = 0;
int lengthOfTask = 120; //list.size();
while (current < lengthOfTask && !isCancelled()) {
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
return "Interrupted";
}
setProgress(100 * current / lengthOfTask);
publish(current + "/" + lengthOfTask);
current++;
}
return "Done";
}
@Override protected void process(List<String> chunks) {
for (String message : chunks) {
monitor.setNote(message);
}
}
@Override public void done() {
runButton.setEnabled(true);
monitor.close();
String text = null;
if (isCancelled()) {
text = "Cancelled";
} else {
try {
text = get();
} catch (Exception ex) {
ex.printStackTrace();
text = "Exception";
}
}
// System.out.println(text);
area.append(text + "\n");
area.setCaretPosition(area.getDocument().getLength());
// appendLine(text);
}
};
worker.addPropertyChangeListener(new ProgressListener(monitor));
worker.execute();
}}
* 解説 [#explanation]
上記のサンプルでは、`SwingWorker`を使ったタスクの進捗状態を`ProgressMonitor`で表示しています。
- `ProgressListener`は`Tutorial`の[https://docs.oracle.com/javase/tutorial/uiswing/examples/components/ProgressMonitorDemoProject/src/components/ProgressMonitorDemo.java ProgressMonitorDemo.java]を変更して使用
#code{{
class ProgressListener implements PropertyChangeListener {
private final ProgressMonitor monitor;
public ProgressListener(ProgressMonitor monitor) {
this.monitor = monitor;
this.monitor.setProgress(0);
}
@Override public void propertyChange(PropertyChangeEvent e) {
Object o = e.getSource();
String strPropertyName = e.getPropertyName();
if ("progress".equals(strPropertyName) && o instanceof SwingWorker) {
SwingWorker task = (SwingWorker) o;
monitor.setProgress((Integer) e.getNewValue());
if (monitor.isCanceled() || task.isDone()) {
task.cancel(true);
}
}
}
}
}}
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/ProgressMonitor.html ProgressMonitor (Java Platform SE 8)]
- [https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html How to Use Progress Bars (The Java™ Tutorials)]
- [[ProgressMonitorがダイアログを表示するまでの待ち時間>Swing/MillisToDecideToPopup]]
-- 処理時間が短くて`ProgressMonitor`が表示されない場合の待ち時間をテスト
- [https://bugs.openjdk.org/browse/JDK-6445283 [JDK-6445283] ProgressMonitorInputStream not large file aware (>2GB) - Java Bug System]
* コメント [#comment]
#comment
#comment