Results 1 to 3 of 3
  1. #1
    Azure is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Question Need help with Timer running in background

    Hi
    I wish to do 2 things.
    when I click the START button two things need to happen
    1 - a timer starts
    2 - an array begins to be created

    2.1 - WHILE and after the array is being created, I want that the time spent is shown in my GUI swing. (...this is my problem...)

    3 - After the array is created, timer stops counting.

    My problem is that I do not know how to make the time spent to be shown WHILE and after the array is being created. It showns ONLY AFTER the array is created.

    Here is the code

    I'm using 2 classes: MainClass (my GUI) and CountClass (extends TimerTask)

    Java Code:
    [COLOR="PaleGreen"]********************************************************[/COLOR]
    
    import java.util.ArrayList;
    
    public class MainClass extends javax.swing.JFrame{
    
        public MainClass() {
            initComponents();
        }
    
    [COLOR="Silver]/*
    private void initComponents()
    {
    ...
    create a button
    create a label
    
    add these things in a container
    ...
    etc etc etc
    }
    */[/COLOR]
    
    [COLOR="silver"]//Here is the interesting code[/COLOR]
    
        private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {                                           
            [COLOR="silver"]/*
             * All I wish to do is, whenever this button is pressed, we start
             * a timer, which need to be showed in 'lbTimer'
             * 'lbTimer' is a javax.swing.JLabel object...
             * However, I do not want the time spent to be showed only after
             * the creation of the array. I want it to be shown at all times
             * updating the time show in lbTimer while at the same time the program
             * is creating the array...
             * 
             * However this is the most I was able to do...
             * It shows time spent in my GUI swing only after the array is created..
             * Before the array is created, my monitor seems like it is frozen...
            */
            
            /* Also, if I run 'notify()' or 'notifyAll()' method here in MainClass
             * or at my 'CountClass', I get 'IllegalMonitorStateException'
             * If you do that, you will see the way I want it to behave
             * However, the array will never be created, and the programm won't end
             */
    
    
    
            //initializing variables[/COLOR]
            int arraySize = 100000;
            long[] array = new long[arraySize];
            
            [COLOR="silver"]//instantiating the timer count object[/COLOR]
            CountClass countObject = new CountClass(0, lbTimer);
    
            [COLOR="silver"]//lbTimer is a label which will be used to show timer value[/COLOR]
            java.util.Timer timer = new java.util.Timer();
    
            [COLOR="silver"]//starting timer![/COLOR]
            timer.scheduleAtFixedRate(countObject,0L, 1L);
    
            
            [COLOR="silver"]/**
             * code to fill 'array' with UNIQUE numbers while scrambling them
             */[/COLOR]
    
    [COLOR="silver"]        //creating a list of unique numbers[/COLOR]
            ArrayList<Integer> list = new ArrayList<Integer>();
            for(int i = 1; i <= arraySize; i++)
                list.add(i);
    
            long aux = 0;
    
            [COLOR="silver"]/**
             * putting those unique numbers in random positions of 'array'.
             * every number used cannot be used twice, since it is
             * removed from 'list'
             */[/COLOR]
            for(int i = 0; i < arraySize; i++)
            {
                aux = (long) (Math.random()*list.size());
                    array[i] = list.get((int)aux);
                    list.remove((int)aux);
            }
    
            /[COLOR="silver"]**
             * finnaly we have created the array I wished. So, now i want
             * to stop the timer
             */[/COLOR]
            countObject.cancel();
    
        } [COLOR="silver"]//end btnStartActionPerformed method[/COLOR]
    
    ...
    ...
    ...
    
    } [COLOR="silver"]//end MainClass[/COLOR]
    
    [COLOR="PaleGreen"]********************************************************[/COLOR]
    import java.text.NumberFormat;
    
    
    public class CountClass extends java.util.TimerTask {
    
        double targ;
        double n;
    
        javax.swing.JLabel lbTimer = new javax.swing.JLabel();
    
        CountClass(double target, javax.swing.JLabel lbTimer)
        {
            this.targ = target;
            this.lbTimer = lbTimer;
        } [COLOR="silver"]//end constructor[/COLOR]
    
        public void run(){
    
            [COLOR="Silver"]/* my label will be updated every 1 millisecond
             * therefore... targ will be increased with 0.001 every time
             * this method is run
             */[/COLOR]
    
            n = this.targ;
            this.targ = this.targ + 0.001;
            String aux = String.valueOf(n);
            aux = NumberFormat.getInstance().format(n);
    
            [COLOR="silver"]//update time shown in GUI swing ( MainClass )[/COLOR]
            lbTimer.setText(aux);
            
        } [COLOR="silver"]// end run method[/COLOR]
    
    } [COLOR="silver"]// end CountClass[/COLOR]
    Moderator Edit: Code tags added
    Last edited by Fubarable; 05-22-2010 at 01:04 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    If this were my project, first of all, I'd look at using the Collections shuffle method to randomize the ArrayList. Next I'd look at doing any long-running tasks in a background thread so as not to freeze my GUI by tying up the EDT or main Swing thread. A SwingWorker object could be perfect for this. I'd also use a Swing Timer for my Timer.

    If you're interested in using these critters, have a look in the Sun Swing tutorials on how to use them. You won't regret this.

    How to use Swing Timers
    Concurrency in Swing
    Collections#shuffle API

Similar Threads

  1. How do I make a timer run in the background?
    By caryr in forum Advanced Java
    Replies: 15
    Last Post: 09-04-2009, 02:34 AM
  2. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  3. get keyboard input while running in the background?
    By gen1mx6 in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2009, 03:51 PM
  4. Running Timer
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 09:24 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •