0

I've got a problem with my simulation of processor counter in java. I want to counting for example from 0 to 6 and show current values of counting (0, 1, 2, 3, 4, 5, 6) in Text field. When I click "Count" button, my program freezes for a moment and after counting in Text field I can see only number 6. I want to see other numbers during the counting. Here is part of my code:

for (int b=0; b<30; b++)
        {
           counter2.Count();
           try {
                    Thread.sleep(300);
                } catch(InterruptedException e){}
           text6.setText("TEST" + counter2.MainReg); }

What can I do with it? Thank you for helping me.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Martyn
  • 21
  • 3

1 Answers1

7

You need to put your calculations on one of the worker threads. From Oracle:

When a Swing program needs to execute a long-running task, it usually uses one of the worker threads, also known as the background threads. Each task running on a worker thread is represented by an instance of javax.swing.SwingWorker. SwingWorker itself is an abstract class; you must define a subclass in order to create a SwingWorker object; anonymous inner classes are often useful for creating very simple SwingWorker objects.

Here's the link you can go to and learn how to do it:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

Here's an example by Oracle on how to implement methods from the SwingWorker class. http://docs.oracle.com/javase/tutorial/uiswing/concurrency/simple.html

Since you want to be able to see results as the process happens you can follow the following example from Oracle: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/interim.html

EDIT: As suggested by mKorbel in the comment below, you can also use Swing Timers as described here: http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

Nico
  • 3,471
  • 2
  • 29
  • 40
  • Is it a lot of work? Have I define a new class or only modify my GUI class? – Martyn Apr 15 '13 at 18:55
  • 1
    @user2283763 No its not a lot of work. Go to the second link I provided to see an example by Oracle on how methods from the SwingWorker class can be implemented. Or go to the last link if you want to use Swing Timers for this task. Either way, you just need to follow simple steps in those pages from Oracle and your program will work fine. You can do all of this from within the class you already have. – Nico Apr 15 '13 at 18:57
  • If I paste here my GUI class code can you help me with this? I stuck in this moment for hours and I can't do anything. My English is not good and I don't understand a lot of things in that documents. I need this for tommorow :) – Martyn Apr 15 '13 at 19:05
  • @user2283763 Why what's tomorrow? Also, if you intend to program in Java (or most any programming language), you need to be able to read the documentation. – Nico Apr 15 '13 at 19:07
  • Cause I need to show my program in university. I can do this counting in console (I've got a class ConsoleTester) but I want to show counting in my GUI. This is last thing that i need to do in my program and I can't do it fine. – Martyn Apr 15 '13 at 19:10
  • Ah, nope. Can't do your university work for you. Its against the policies of this site and the point of these exercises is so you'd learn...not show that you can copy-paste. I will see if I can provide an example but other than that the above links are plenty for you to be able to implement these. If you can do it in console then you can also do it with the GUI. Just need to focus on what docs are saying. – Nico Apr 15 '13 at 19:12
  • 1
    @user2283763 Follow the link provided. May be you can use some of it. [Swing timer not stopping](http://stackoverflow.com/questions/14409868/swing-timer-not-stopping/14410163#14410163) – Smit Apr 15 '13 at 19:12