Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Timer inside loop

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Timer inside loop

    Hello. On button click, I have some loop and inside it I want to append jTextArea with some data. I want to do it for example every 3 seconds. For example:
    answer1 printed 11:13:10
    answer1 printed 11:13:13
    answer3 printed 11:13:16
    answer4 printed 11:13:19

    and so on.

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            if (kiek > 0 && jTextArea1.getLineCount() > 0) {
                String str;
     
                BufferedReader reader = new BufferedReader(
                        new StringReader(jTextArea1.getText()));
                double prog = 0;
                try {
                    while ((str = reader.readLine()) != null) {
     
                        if (str.length() > 0) {
                            jTextArea2.append("---------------------\n");
     
                            jTextArea2.append("Working with IP " + str + "\n");
                            for (int j = 0; j < a.size(); j++) {
     
                                jTextArea2.append(a.get(j) + "\n");
                            }
                        }
                        prog += (double) 100 / (double) jTextArea1.getLineCount();
                        jProgressBar1.setValue((int) (prog));
                    }
     
                } catch (IOException ex) {
                }
            }
     
        }


    I get each line of other TextArea, print it to current, and also add some information from ArrayList a jTextArea2.append(a.get(j) + "\n");

    All I want is that every output to textarea would be periodical for example for 3 seconds.
    I tried to do it with Thread.pause(); but in that way, it is waiting for all function to end, and then outputs all data in one time.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Timer inside loop

    Swing is single threaded, bog down that single thread with sleep or pause and you bog down that thread and lock up the gui. Use a swing timer, which seems more appropriately suited for your needs

Similar Threads

  1. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 10:50 PM
  2. how to set timer
    By jack_nutt in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 08:55 PM
  3. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 08:48 PM
  4. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  5. How to Use Timer in Java
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM