-1

I have a program that I am trying to execute a task in, wait a second, move a progress bar, and then execute another task, wait, progress bar, execute, etc.

If I use

Thread.sleep()

it freezes my GUI, not allowing my progress bar to move. I have tried the following questions:

How to set a timer in java

Make something wait without using Thread.sleep()?

Make a java program sleep without threading

My code is similar to this:

import java.util.*

...

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec("cmd /c del file");
                } catch (IOException ex) {
                    Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, 1000);
        progressBar.setValue(16);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec("cmd /c del file");
                } catch (IOException ex) {
                    Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, 1000);
        progressBar.setValue(32);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec("cmd /c del file");
                } catch (IOException ex) {
                    Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, 1000);
        progressBar.setValue(48);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec("cmd /c del file");
                } catch (IOException ex) {
                    Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, 1000);
        progressBar.setValue(64);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec("cmd /c del file");
                } catch (IOException ex) {
                    Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, 1000);
        progressBar.setValue(80);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    Runtime.getRuntime().exec("cmd /c del file");
                } catch (IOException ex) {
                    Logger.getLogger(AdminHome.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }, 1000);
        progressBar.setValue(100);
        progressBar.setString("Done!");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

            }
        }, 1000);
        System.exit(0);

Thanks in advance for any help.

Riccorbypro

Community
  • 1
  • 1
Riccorbypro
  • 143
  • 2
  • 8
  • You will need to run your GUI in a seperate thread than your tasks – Arno_Geismar Jul 17 '14 at 09:36
  • `it freezes my GUI, not allowing my progress bar to move.` You need to implement background tasks (waiting included) in a separate thread. Take a look on [this question](http://stackoverflow.com/questions/2668389/java-gui-need-to-pause-a-method-without-freezing-gui-aswell) – default locale Jul 17 '14 at 09:37
  • @defaultlocale I have tried a SwingWorker, to no avail. What else can I try? – Riccorbypro Jul 17 '14 at 09:55
  • @Riccorbypro What did go wrong with SwingWorker? You can try creating [Threads](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html) by yourself, but `SwingWorker.doInBackground` should do the same thing. – default locale Jul 17 '14 at 10:19
  • I'm using NetBeans IDE, so when the code underlines in red, I know that something is wrong. After I inserted the SwingWorker, using the knowledge gained from the Java API, everything became red, and my program would not even run. I then deleted the code segment, and it ran just fine again. – Riccorbypro Jul 17 '14 at 14:12

1 Answers1

0

Any processing should occur on a thread separate to the GUI thread (the EDT), to ensure that when you do call sleep on a thread, the GUI is still responsive. So, whatever you're doing to measure progress, or emulate it, should be occurring in a different thread to the GUI.

Take a look at:

Swing Workers

Progress Bars