1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw;
19
20 import java.awt.BorderLayout;
21 import java.awt.Color;
22 import java.lang.reflect.InvocationTargetException;
23
24 import javax.swing.BorderFactory;
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27 import javax.swing.JProgressBar;
28 import javax.swing.SwingConstants;
29 import javax.swing.SwingUtilities;
30
31 /***
32 * A simple ProgressPanel that can be used, a little more flexible
33 * than ProgressMonitor when you want it to be shown REGARDLESS
34 * of any timeouts etc.
35 *
36 * @author Paul Smith <psmith@apache.org>
37 *
38 */
39 public class ProgressPanel extends JPanel {
40 private final JLabel messageLabel = new JLabel();
41 private final JProgressBar progressBar;
42
43 ProgressPanel(int min, int max, String msg) {
44 this.progressBar = new JProgressBar(min, max);
45 setBorder(BorderFactory.createLineBorder(Color.black, 1));
46 messageLabel.setHorizontalAlignment(SwingConstants.HORIZONTAL);
47 messageLabel.setText(msg);
48 setLayout(new BorderLayout());
49
50 add(progressBar, BorderLayout.CENTER);
51 add(messageLabel, BorderLayout.SOUTH);
52 }
53
54 public void setMessage(final String string) {
55 SwingUtilities.invokeLater(
56 new Runnable() {
57 public void run() {
58 messageLabel.setText(string);
59 }
60 });
61 }
62
63 public void setProgress(final int progress) {
64 try {
65 Runnable runnable = new Runnable() {
66 public void run() {
67 progressBar.setValue(progress);
68 }
69 };
70 if (!SwingUtilities.isEventDispatchThread()) {
71 SwingUtilities.invokeAndWait(runnable);
72 }else {
73 runnable.run();
74 }
75 } catch (InterruptedException e) {
76
77 e.printStackTrace();
78 } catch (InvocationTargetException e) {
79
80 e.printStackTrace();
81 }
82 }
83 }