0

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.

mutantkeyboard
  • 1,614
  • 1
  • 16
  • 44

2 Answers2

0

I'm not really sure about your question... but this does work:

public void iterate() {
    while (num < 2000) {            
        current.setValue(num);
        try {
            Thread.sleep(500);

        } catch (InterruptedException e) {
        }

        num += 95;

        if (num >= 2000)
            System.exit(0);
    }
}
Dani
  • 3,744
  • 4
  • 27
  • 35
  • Hmm... weird, but thanks. In my case it didn't work at all,but I found a similar solution `if(current.getValue() >= (2000-95)){ System.exit(0);` which actually worked. – mutantkeyboard Dec 27 '12 at 17:37
  • Maybe you put the if statement before `num += 95`? If didn't, I can't figure out why the code didn't work – Dani Dec 27 '12 at 17:40
  • 5
    `Thread.sleep(500);` Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Dec 27 '12 at 17:51
0

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;

}
imxylz
  • 7,847
  • 4
  • 28
  • 25