The following short program should display the ProgressMonitor when the Run button is pushed. However, it displays an empty dialog box, which goes away when the loop is finished. So it seems to half work. I have run out ideas as to why it doesn't work properly. Somebody suggested that I start the loop in another thread, but that didn't solve the problem. Has anybody had this problem before? Can anybody help me?
Many thanks in advance. Gord
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame implements ActionListener {
private JButton runButton = new JButton("Run"),
quitButton = new JButton("Quit");
public ProgressMonitor progressMonitor;
public static void main(String[] args) {
new Test();
}
private Test() {
super("Test Progress Monitor");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(runButton);
runButton.addActionListener(this);
getContentPane().add(quitButton);
quitButton.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == runButton) {
progressMonitor = new ProgressMonitor(Test.this,
"Test progress monitor", null, 1, 100);
progressMonitor.setMillisToDecideToPopup(0);
progressMonitor.setMillisToPopup(0);
progressMonitor.setProgress(0);
for (int i = 1; i <= 100; i ++) {
try {Thread.sleep(10);} catch(InterruptedException e) {}
progressMonitor.setProgress(i);
}
} else if (event.getSource() == quitButton) {
dispose();
System.exit(0);
}
repaint();
}
}
Many thanks in advance. Gord
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JFrame implements ActionListener {
private JButton runButton = new JButton("Run"),
quitButton = new JButton("Quit");
public ProgressMonitor progressMonitor;
public static void main(String[] args) {
new Test();
}
private Test() {
super("Test Progress Monitor");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(runButton);
runButton.addActionListener(this);
getContentPane().add(quitButton);
quitButton.addActionListener(this);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == runButton) {
progressMonitor = new ProgressMonitor(Test.this,
"Test progress monitor", null, 1, 100);
progressMonitor.setMillisToDecideToPopup(0);
progressMonitor.setMillisToPopup(0);
progressMonitor.setProgress(0);
for (int i = 1; i <= 100; i ++) {
try {Thread.sleep(10);} catch(InterruptedException e) {}
progressMonitor.setProgress(i);
}
} else if (event.getSource() == quitButton) {
dispose();
System.exit(0);
}
repaint();
}
}