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.
You can check the javadoc while building the JFrame:
public interface WindowConstants
{
/**
* The do-nothing default window close operation.
*/
public static final int DO_NOTHING_ON_CLOSE = 0;
/**
* The hide-window default window close operation
*/
public static final int HIDE_ON_CLOSE = 1;
/**
* The dispose-window default window close operation.
* <p>
* <b>Note</b>: When the last displayable window
* within the Java virtual machine (VM) is disposed of, the VM may
* terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
* AWT Threading Issues</a> for more information.
* @see java.awt.Window#dispose()
* @see JInternalFrame#dispose()
*/
public static final int DISPOSE_ON_CLOSE = 2;
/**
* The exit application default window close operation. Attempting
* to set this on Windows that support this, such as
* <code>JFrame</code>, may throw a <code>SecurityException</code> based
* on the <code>SecurityManager</code>.
* It is recommended you only use this in an application.
*
* @since 1.4
* @see JFrame#setDefaultCloseOperation
*/
public static final int EXIT_ON_CLOSE = 3;
}