Ok people, I have this sample Java code. Many of you have probably seen it before. Since I'm very new to Java I wondered how do you actually invoke a program to close after the ProgressBar reaches 100% or in my case num >= 2000?
Code:
package progress;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ProgressMonitor extends JFrame {
JProgressBar current;
JTextArea out;
JButton find;
Thread runner;
int num = 0;
public ProgressMonitor()
{
super("Progress monitor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(205,68);
setLayout(new FlowLayout());
current = new JProgressBar(0,2000);
current.setValue(0);
current.setStringPainted(true);
add(current);
}
public void iterate()
{
while(num<2000){
current.setValue(num);
try{
Thread.sleep(1000);
}catch (InterruptedException e) { }
num+=95;
}
}
public static void main(String[] args) {
ProgressMonitor pm = new ProgressMonitor();
pm.setVisible(true);
pm.iterate();
}
}
I tried with the if statement in while block so I written
if(num >=2000) System.exit(0);
but nothing happened.
I also tried with converting the JProgressBar getValue() method and boxing it as an integer
if ((Integer)current.getValue() >= 100) System.exit(0);
and the one where the current.getValue() >= 2000 as well but neither worked for me.
Can you help me find a solution? Thank you in advance.